Javascript日期简述

2022/9/15 1:17:31

本文主要是介绍Javascript日期简述,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

概述

实例化日期有四种方式

var d = new Date();
var d = new Date(milliseconds);
var d = new Date(dateString);
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);

JavaScript 日期格式

类型 实例
ISO 日期 "2018-02-19" (国际标准)
短日期 "02/19/2018" 或者 "2018/02/19"
长日期 "Feb 19 2018" 或者 "19 Feb 2019"
完整日期 "Monday February 25 2015"

获取方法

方法 描述
getFullYear() 获取四位的年(yyyy)
getMonth() 获取月(0-11)
getDate() 以数值返回天(1-31)
getDay() 以数值获取周名(0-6)
getHours() 获取小时(0-23)
getMinutes() 获取分(0-59)
getSeconds() 获取秒(0-59)
getMilliseconds() 获取毫秒(0-999)
getTime() 获取时间(从 1970 年 1 月 1 日至今)

英文月份: "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"

英文星期:"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"

日期设置方法

方法 描述
setFullYear() 设置年(可选月和日)
setMonth() 设置月(0-11)
setDate() 以数值(1-31)设置日
setHours() 设置小时(0-23)
setMinutes() 设置分(0-59)
setSeconds() 设置秒(0-59)
setMilliseconds() 设置毫秒(0-999)
setTime() 设置时间(从 1970 年 1 月 1 日至今的毫秒数)

实例

创建日期

  • 现在时间
var now = new Date();
  • 指定日期
// 2022年5月31号 13点14分15秒
var definedDt = new Date(2022, 4, 31, 13, 14, 15);
console.info("definedDt:", definedDt);
console.info("mills:", definedDt.getTime());

// definedDt: Date Tue May 31 2022 13:14:15 GMT+0800 (中国标准时间)
// mills: 1653974055000
  • 指定日期前后
    之后n天,之前n天,之后n月等等
// 之后一天
var next = new Date(1653974055000);
next.setDate(next.getDate() + 1);
console.info("next day:", next);
// 之前一天
var before = new Date(1653974055000);
before.setDate(before.getDate() - 1);
console.info("before day:", before);
// next day: Date Wed Jun 01 2022 13:14:15 GMT+0800 (中国标准时间)
// before day: Date Mon May 30 2022 13:14:15 GMT+0800 (中国标准时间)


// 5月31号 和 6月1号的下一个月都是7月1号
var may31 = new Date(2022, 4, 31);	
may31.setMonth(may31.getMonth() + 1);
console.info("next month:", may31);
var jun01 = new Date(2022, 5, 1);
jun01.setMonth(jun01.getMonth() + 1);
console.info("next month:", jun01);

// next month: Date Fri Jul 01 2022 00:00:00 GMT+0800 (中国标准时间)
// next month: Date Fri Jul 01 2022 00:00:00 GMT+0800 (中国标准时间)

格式化日期

  • JS自带
var may31 = new Date(1653974055000);
console.info("toString: ",may31.toString());
console.info("toDateString: ",may31.toDateString());
console.info("toLocaleString: ",may31.toLocaleString());
console.info("toLocaleDateString: ",may31.toLocaleDateString());
console.info("toGMTString: ",may31.toGMTString());
console.info("toUTCString: ",may31.toUTCString());
console.info("toISOString: ",may31.toISOString());

// toString:  Tue May 31 2022 13:14:15 GMT+0800 (中国标准时间)
// toDateString:  Tue May 31 2022
// toLocaleString:  2022/5/31 13:14:15
// toLocaleDateString:  2022/5/31
// toGMTString:  Tue, 31 May 2022 05:14:15 GMT
// toUTCString:  Tue, 31 May 2022 05:14:15 GMT
// toISOString:  2022-05-31T05:14:15.000Z
  • 自定义转换
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;
}

var may31 = new Date(1653974055123);
var r1 = may31.format("yyyy-MM-dd HH:mm:ss.S");
var r2 = may31.format("yyyy/MM/dd HH:mm:ss");
console.info("r1:", r1);
console.info("r2:", r2);

// r1: 2022-05-31 13:14:15.123
// r2: 2022/05/31 13:14:15

解析日期字符串

使用new Date(dateString)

console.info(new Date("2022-05-31 13:14:15.123"));
console.info(new Date("2022/05/31 13:14:15"));
console.info(new Date("Tue May 31 2022 13:14:15 GMT+0800"));
console.info(new Date("Tue, 31 May 2022 05:14:15 GMT"));
console.info(new Date("2022-05-31T05:14:15.000Z"));
// Date Tue May 31 2022 13:14:15 GMT+0800 (中国标准时间)
// Date Tue May 31 2022 13:14:15 GMT+0800 (中国标准时间)
// Date Tue May 31 2022 13:14:15 GMT+0800 (中国标准时间)
// Date Tue May 31 2022 13:14:15 GMT+0800 (中国标准时间)
// Date Tue May 31 2022 13:14:15 GMT+0800 (中国标准时间)


这篇关于Javascript日期简述的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程