整理 js 日期对象的详细功能,使用 js 日期对象获取具体日期、昨天、今天、明天、每月...
source link: https://www.cnblogs.com/jiangweiping/p/16950035.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
整理 js 日期对象的详细功能,使用 js 日期对象获取具体日期、昨天、今天、明天、每月天数、时间戳等,以及常用的日期时间处理方法
在 javascript 中内置了一个 Date 对象,可用于实现一些日期和时间的操作。
本文整理 js 日期对象的详细功能,使用 js 日期对象获取具体日期、昨天、今天、明天、每月天数、时间戳等,以及常用的日期时间处理方法。
在前端可以通过new Date()生成Date对象,如果没有传参数时,即获取本地当前日期和时间。不过这时候显示的内容并不是我们常见的日期格式,而是一个当前时区时间的描述文本,以下代码显示的效果如图所示:
如果希望显示的内容格式化为常见的日期格式,最简单的办法是调用 toLocaleString() 方法,如下所示:
const date = new Date(); console.log(date.toLocaleString());
new Date()可接受三种类型的参数
第一种是只传入一个number类型的参数,一般是时间戳的毫秒数,返回参数数字所处的时间,如下所示:
const date = new Date(946684800000); console.log(date.toLocaleString());
第二种也只传入一个参数,参数类型是string,不过需要是正确的格式,如 "2010-10-10 10:10:10",返回对应的时间:
const date = new Date("2010-10-10 10:10:10"); console.log(date.toLocaleString());
const date = new Date(2022,12,1,10,10,10,999); console.log(date.toLocaleString());
const date = new Date(); console.log(date.getTime());
console.log(Date.now());
const date = new Date(); console.log(date.valueOf());
const date = new Date(); console.log(+date);
二、获取常用时间数据
function formatDate(){ // 创建日期对象 const date = new Date(); // 获取各日期/时间数据 let year = date.getFullYear(); let month = date.getMonth(); let DD = date.getDate(); let hour = date.getHours(); let minute = date.getMinutes(); let second = date.getSeconds(); let day = date.getDay(); // 拼接日期时间为字符串 let time = year + '年' + month + '月' + DD + '日 ' + hour + ':' + minute + ':' + second + ' 星期' + ['日','一','二','三','四','五','六',][day]; return time } console.log(formatDate())
三、设置日期时间
const date = new Date(); date.setFullYear(2020) console.log(date.toLocaleString());
const date = new Date(); date.setMonth(2,0); console.log(date.getDate());
const date = new Date(); date.setDate(date.getDate() + 1); console.log('明天是',date.toLocaleString()); date.setDate(date.getDate() - 2); console.log('昨天是',date.toLocaleString());
function getSpecificDate(day){ //计算出要加/减的毫秒数 var num = 1000*60*60*24*day; var newDate = new Date(Date.now()+num); return newDate; } console.log('明天是',getSpecificDate(1).toLocaleString()); console.log('后天是',getSpecificDate(2).toLocaleString()); console.log('昨天是',getSpecificDate(-1).toLocaleString());
还有一种很方便的,给Date的原型方法中加入format方法,这样在Date对象上可以直接使用format方法格式化所需要的日期。如下所示:
Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, //month "d+" : this.getDate(), //day "h+" : this.getHours(), //hour "m+" : this.getMinutes(), //minute "s+" : this.getSeconds(), //second "q+" : Math.floor((this.getMonth()+3)/3), //quarter "S" : this.getMilliseconds() //millisecond }; if(/(y+)/.test(format)){ format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); } for(var k in o) { if(new RegExp("("+ k +")").test(format)){ format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length)); } } return format; }; console.log(new Date().format('yyyy-MM-dd hh:mm:ss'))
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK