js时间搓化为今天明天_js对时间戳的处理 获取时间,昨天,今天,明天,时间不同格式...

1.获取昨天,今天,明天的时间

//昨天的时间

var day1 = new Date();

day1.setTime(day1.getTime()-24*60*60*1000);

var s1 = day1.getFullYear()+"-" + (day1.getMonth()+1) + "-" + day1.getDate();

//今天的时间

var day2 = new Date();

day2.setTime(day2.getTime());

var s2 = day2.getFullYear()+"-" + (day2.getMonth()+1) + "-" + day2.getDate();

//明天的时间

var day3 = new Date();

day3.setTime(day3.getTime()+24*60*60*1000);

var s3 = day3.getFullYear()+"-" + (day3.getMonth()+1) + "-" + day3.getDate();

//拼接时间

function show(){

var str = "" + s1 + "至" + s2;

return str;

}

//赋值doubleDate

$('#dateS').val(show());

2.时分秒,星期,可单个获取

function writeCurrentDate() {

var now = new Date();

var year = now.getFullYear(); //得到年份

var month = now.getMonth();//得到月份

var date = now.getDate();//得到日期

var day = now.getDay();//得到周几

var hour = now.getHours();//得到小时

var minu = now.getMinutes();//得到分钟

var sec = now.getSeconds();//得到秒

var MS = now.getMilliseconds();//获取毫秒

var week;

month = month + 1;

if (month < 10) month = "0" + month;

if (date < 10) date = "0" + date;

if (hour < 10) hour = "0" + hour;

if (minu < 10) minu = "0" + minu;

if (sec < 10) sec = "0" + sec;

if (MS < 100)MS = "0" + MS;

var arr_week = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");

week = arr_week[day];

var time = "";

time = year + "年" + month + "月" + date + "日" + " " + hour + ":" + minu + ":" + sec + " " + week;

//当前日期赋值给当前日期输入框中(jQuery easyUI)

$("#currentDate").html(time);

//设置得到当前日期的函数的执行间隔时间,每1000毫秒刷新一次。

var timer = setTimeout("writeCurrentDate()", 1000);

}

我之前的一个小案例,就主要用这个写的。小案例

3.可以直接对年月日时分秒进行操作

//昨天的时间

var day1 = new Date();

day1.setDate(day1.getDate() - 1);

var s1 = day1.format("yyyy-MM-dd");

//前天的时间

var day2 = new Date();

day2.setDate(day2.getDate() - 2);

var s2 = day2.format("yyyy-MM-dd");

4.format函数为扩展函数

/**

*对Date的扩展,将 Date 转化为指定格式的String

*月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,

*年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)

*例子:

*(new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423

*(new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18

*/

Date.prototype.format = function (fmt) {

var o = {

"M+": this.getMonth() + 1, //月份

"d+": this.getDate(), //日

"h+": this.getHours(), //小时

"m+": this.getMinutes(), //分

"s+": this.getSeconds(), //秒

"q+": Math.floor((this.getMonth() + 3) / 3), //季度

"S": this.getMilliseconds() //毫秒

};

if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));

for (var k in o)

if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));

return fmt;

}

5.页面日期 2019-06-27 变 20190627      “2019-06-27”.replace(/-/g,"")

6.更多方法

// 获取某个时间格式的时间戳

var date = new Date(); //时间对象

var str = date.getTime(); //转换成时间戳

var time = new Date(“2014-07-10 10:21:12”);

var str = time.getTime();

// 将时间戳换成时间格式字符串

var timestamp3 = 1403058804; //注意:要是拿到的是字符串,可以通过parseInt等方法转成数字才可以

var newDate = new Date();

newDate.setTime(timestamp3 * 1000);

// Wed Jun 18 2014

console.log(newDate.toDateString());

// Wed, 18 Jun 2014 02:33:24 GMT

console.log(newDate.toGMTString());

// 2014-06-18T02:33:24.000Z

console.log(newDate.toISOString());

// 2014-06-18T02:33:24.000Z

console.log(newDate.toJSON());

// 2014年6月18日

console.log(newDate.toLocaleDateString());// 2014年6月18日 上午10:33:24

console.log(newDate.toLocaleString());

// 上午10:33:24

console.log(newDate.toLocaleTimeString());

// Wed Jun 18 2014 10:33:24 GMT+0800 (中国标准时间)

console.log(newDate.toString());

// 10:33:24 GMT+0800 (中国标准时间)

console.log(newDate.toTimeString());

// Wed, 18 Jun 2014 02:33:24 GMT

console.log(newDate.toUTCString());

封装的函数,可直接调用:

Base.RSFormatDat = function(dt, it) {

var _tTime = new Date(parseInt(dt) * 1000);

if (it == 0) {

var tHours = _tTime.getHours();

if (tHours < 10 && tHours >= 0) {

tHours = “0” + tHours;

}

var tMinutes = _tTime.getMinutes();

if (tMinutes < 10 && tMinutes >= 0) {

tMinutes = “0” + tMinutes;

}

var tSeconds = _tTime.getSeconds();

if (tSeconds < 10 && tSeconds >= 0) {

tSeconds = “0” + tSeconds;

}

return _tTime.getFullYear() + “年” + (parseInt(_tTime.getMonth(), 10) + 1) + “月” + _tTime.getDate() + "日 " + tHours + “:” + tMinutes + “:” + tSeconds;

} else {

return _tTime.getFullYear() + “年” + (parseInt(_tTime.getMonth(), 10) + 1) + “月” + _tTime.getDate() + “日”;

}

}

Base.RSFormatDat (第一个参数是要转化的时间戳,第二个参数是转化成决定要转成年月日还是年月日时分秒,前者传入1,后者传入0)

例如 把 1510151706转化成 x年x月x日

Base.RSFormatDat (1510151706,0)

console.log(newDate.toLocaleDateString());这个很好用

延申阅读 神奇的 toLocaleString

时间戳在线转换工具 其实有超过10位的时间戳,是会显示毫秒的

Java时间戳转化为今天、昨天、明天(字符串格式)

原文:http://www.open-open.com/code/view/1435301895825 时间戳,相信大家一定都不陌生,服务器经常会传回来时间戳,需要我们对时间戳进行处理.各种麻烦不断, ...

js获得昨天前天明天时间以及setDate&lpar;&rpar;

python获取当前,昨天,明天时间

import datetime nowTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')#现在 pastTimeMinutes = ...

java获取日期 昨天 今天 明天的日期

Date date=new Date();//取时间 Calendar calendar = new GregorianCalendar(); calendar.setTime(date); cale ...

python中获取今天昨天和明天的日期

import datetime today = datetime.date.today()oneday = datetime.timedelta(days=1)yesterday = today-on ...

js中时间戳转换成时间格式

js中时间戳转换成时间格式, // 时间戳转换成时间格式 var formatDate = function(date){ date = new Date(date); var y=date.getF ...

js格式化时间 js格式化时间戳

一个js格式化时间和js格式化时间戳的例子. 代码:/** * 时间对象的格式化; */Date.prototype.format = function(format) { /* * eg:forma ...

js获取当天时间&comma;7天前后时间&comma;时间格式化

格式化时间年月日时分秒 //时间戳转换方法 date:时间戳数字 formatDate(date) { var date = new Date(date); var YY = date.getFull ...

jsp采用数据库连接池的方法获取数据库时间戳context&period;xml配置,jsp页面把时间格式化成自己需要的格式

<?xml version="1.0" encoding="UTF-8"?>

随机推荐

在C&num;项目中需要用double类型操作MSSQL float类型数据(附C&num;数据类型和SQL数据类型对照)

C#操作SQL Float类型,数据会多很多小数,原来是C#的float和sql的float类型不一致.以下是数据库中与C#中的数据类型对照. /// /// 数据库中 ...

spring的使用《一》

在前边的文章中说明了,如何搭建一个spring的开发环境,简单回顾下就是把spring的jar包导入工程中,如果是在javaWeb项目中是放在lib目录下,然后在web.xml文件中进行配置,配置sp ...

C&num;调用C&plus;&plus;DLL的小总结5---和C&plus;&plus;的DLL的联合调试

http://fpcfjf.blog.163.com/blog/static/5546979320134922938373/ http://blog.csdn.net/jiangxinyu/artic ...

kali 密码攻击

第八章 密码攻击 作者:Willie L. Pritchett, David De Smet 译者:飞龙 协议:CC BY-NC-SA 4.0 这一章中,我们要探索一些攻击密码来获得用户账户的方式.密 ...

高德地图iOS SDK限制地图的缩放比例

问题 高德地图的iOS SDK 3D版中(v2.4.0), 显示范围在560m左右时建筑会呈现3D效果. 我们有没有办法可以限制地图最小缩放到这个比例, 从而保证建筑始终使用3D效果显示呢? 探索 高 ...

UVA 10763 Foreign Exchange

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Description Your non- ...

Linux for周期运行命令注意事项

假定for有一些符号循环指令,需要使用()封闭. for i in {1..4}; do (python /data/UGCRobot/manage/Scheduler.py 1.log > / ...

【转】动态规划DP

[数据结构与算法] DP 动态规划 介绍 原创 2017年02月13日 00:42:51 最近在看算法导论. DP全称是dynamic programming,这里programming不是编程,是一 ...

11&period;14 luffycity项目&lpar;6&rpar;

2018-11-14 21:26:45 实现了购物车功能! 涉及到了redis的使用  需要在pycharm中下载   django_redis 其他的看一下笔记,有购物车里面数据存储的结构才发现数据 ...

重绘和回流(reflow和repaint)

由于DOM操作会导致浏览器的回流,回流需要花费大量的时间进行样式计算和节点重绘与渲染,所以应当尽量减少回流次数. 以下是几种常见的减少重绘和回流的方法: 一.不要一项一项的更改页面的样式,尽量一口气写 ...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值