鳄鱼笔记(一) --> javaScript<--[JS常用時間语句]

1>時間語法

◆基础

1.获取 年月日分秒
// An highlighted block
//今天的时间
	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();//获取毫秒
2. 字符串转为日期
// An highlighted block 2020-08-03
var date = startdate.replace(/-/g,'/');
var temp = new   Date(date);
4. 日期转为字符串
// 固定格式
	console.log(now.toString());  //Mon Nov 11 2019 11:19:12 GMT+0800 (中国标准时间)
    console.log(now.toISOString());  //2019-11-11T03:19:12.534Z
    console.log(now.toTimeString());  //11:19:12 GMT+0800 (中国标准时间)
    console.log(now.toUTCString());  //Mon, 11 Nov 2019 03:19:12 GMT
    console.log(now.toLocaleDateString());  //2019/11/11
    console.log(now.toLocaleString());        //2019/11/11 上午11:19:12
    console.log(now.toLocaleTimeString());  //上午11:19:12
//自定義格式 20200821080000
timeStr1 = now.getFullYear() + ("0" + (now.getMonth()+1)).slice(-2) + ("0" + now.getDate()).slice(-2) +"080000";    

◆简单計算

1.日期加減法-天
// 當前日期 20200821080000
now2= new Date();
now3= new Date();
now2.setDate(now2.getDate()+1);
now3.setDate(now3.getDate()-1);
//20200822080000
timeStr1 = now2.getFullYear() + ("0" + (now2.getMonth()+1)).slice(-2) + ("0" + now2.getDate()).slice(-2) +"080000";
//20200820080000
timeStr2 =  now3.getFullYear() + ("0" + (now3.getMonth()+1)).slice(-2) + ("0" + now3.getDate()).slice(-2) +"080000";

2.日期加減法-月
// 當前日期 20200821080000
now2= new Date();
now3= new Date();
now2.setMonth(now2.getMonth() + 1);
now3.setMonth(now2.getMonth() - 1);
//20200921080000
timeStr1 = now2.getFullYear() + ("0" + (now2.getMonth()+1)).slice(-2) + ("0" + now2.getDate()).slice(-2) +"080000";
//20200721080000
timeStr2 =  now3.getFullYear() + ("0" + (now3.getMonth()+1)).slice(-2) + ("0" + now3.getDate()).slice(-2) +"080000";

3.日期加減法-年
// 當前日期 20200821080000
now2= new Date();
now3= new Date();
now2.setYear(now2.getFullYear() + 1);
now3.setYear(now2.getFullYear() - 1);
//20210821080000
timeStr1 = now2.getFullYear() + ("0" + (now2.getMonth()+1)).slice(-2) + ("0" + now2.getDate()).slice(-2) +"080000";
//20190821080000
timeStr2 =  now3.getFullYear() + ("0" + (now3.getMonth()+1)).slice(-2) + ("0" + now3.getDate()).slice(-2) +"080000";

◆時間間隔計算

1.計算相隔小時數
// 當前日期  2020-08-23 08:00:00  和 now3 2020-08-23 10:30:00
var date1= new Date(now2.replace(/-/g,'/'));
var date2= new Date(now3.replace(/-/g,'/'));
var hours=(date2- date1)/(1*1*60*60*1000)
console.log(days);  // hours= 2.5
//alert(days);
//20210823080000
timeStr1 = 
date1.getFullYear() + 
("0" + (date1.getMonth()+1)).slice(-2) + 
("0" + date1.getDate()).slice(-2) +
("0" + (date1.getHours())).slice(-2)+
("0" + (date1.getMinutes())).slice(-2)+
("0" + (date1.getSeconds())).slice(-2);
//20200823103000
timeStr2 = 
date2.getFullYear() + 
("0" + (date2.getMonth()+1)).slice(-2) + 
("0" + date2.getDate()).slice(-2) +
("0" + (date2.getHours())).slice(-2)+
("0" + (date2.getMinutes())).slice(-2)+
("0" + (date2.getSeconds())).slice(-2);
2.計算相隔天數
// 當前日期  2020-08-20  和 now3 2020-08-23
var date1= new Date(now2.replace(/-/g,'/'));
var date2= new Date(now3.replace(/-/g,'/'));
var days=(date2- date1)/(1*24*60*60*1000)
console.log(days);  // days = 3
//alert(days);
//20210821080000
timeStr1 = now2.getFullYear() + ("0" + (now2.getMonth()+1)).slice(-2) + ("0" + now2.getDate()).slice(-2) +"080000";
//20190823080000
timeStr2 =  now3.getFullYear() + ("0" + (now3.getMonth()+1)).slice(-2) + ("0" + now3.getDate()).slice(-2) +"080000";

博文鏈接

下一篇 鳄鱼笔记(二) --> javaScript<–[JS常用JSON数据处理function]

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值