JavaScript中Date操作

// Date:日期

// 创建一个日期对象
// 无参数的时候,代表当前时间.
var date = new Date();
console.log(date);

var date2 = new Date(1991, 11, 11, 0, 0, 0);
console.log(date2);

// 获取年月日时分秒毫秒
var year = date.getFullYear();
// 月份从0开始
var month = date.getMonth();
var day = date.getDate();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
var milliseconds = date.getMilliseconds();
console.log(year + "/" + (month + 1) + "/" + day + " " + hours + ":" + minutes + ":" + seconds + ":" + milliseconds);
// 一周第几天,0~6
var week = date.getDay();
console.log(week);

// 返回UTC与当前时间的差值.单位分钟
var offset = date.getTimezoneOffset();
console.log(offset);

// 获取当前时间的毫秒数.以1970年1月1日0:0:0为时间基准
var time = date.getTime();
console.log(time);

// 将日期对象转化为字符串
var str = date.toString();
console.log(str);

// 设置年月日时分秒
date.setFullYear(2017);
date.setMonth(3);
date.setDate(15);
date.setHours(18);
date.setMinutes(45);
date.setSeconds(10);
date.setMilliseconds(150);
console.log(date);

// 静态方法
// 获取当前时间的毫秒数.以1970年1月1日0:0:0为时间基准
console.log(Date.now());

猜你喜欢

转载自blog.csdn.net/qq_35134066/article/details/82431650