var now=new Date();
console.log(now);
// Sun Apr 08 2018 14:38:48 GMT+0800 (中国标准时间)
console.log(now.getDate());
// 当前日 8
console.log(now.getMonth()+1);
//当前月 4
console.log(now.getFullYear());
//当前年 2018
console.log(now.getTime());
//当前时间戳(1970年到现在) 1523169789892
console.log(Date.parse("2018 05 12"));
console.log(Date.parse("05 12 2018"));
console.log(Date.parse("May 12,2018"));
console.log(Date.parse("2018,05,12"));
console.log(Date.parse("2018/05/12"));
//1970/01/01-2018/05/12的毫秒数 1526054400000
console.log(now.valueOf());
//返回当前时间的原始值,相当于上边的时间戳 1523169789892
var now=new Date();console.log(now.setDate(1));
//设置时间为本月的第一天,返回时间戳
//同样可以设置月,年等
console.log()
Date的构造函数
var date=new Date();
var date=new Date(string);//参照上边的时间戳格式
var date=new Date(year,month-1,day,hours,minutes,seconds);
var date=new Date(时间戳);
相关方法整理
//将date转换为特定的时间格式,可拓展
function dateToString(date, format) {
var yearstr = date.getFullYear();
var imonth = date.getMonth() + 1;
var monthstr = imonth + "";
if (imonth < 10) {
monthstr = "0" + monthstr;
}
var daystr = date.getDate();
if (daystr < 10) {
daystr = "0" + daystr;
}
if (format == "yyyymm") {
return yearstr + "年" + monthstr + "月";
}
else if (format == "yyyyss") {
return yearstr + "年" + parseInt(imonth / 3 + 1) + "季度";
}
else if (format == "yyyy") {
return yearstr + "年";
}
else if (format == "yyyyhh") {
return yearstr + "年" + (imonth > 6 ? "下半年" : "上半年");
}
else if (format == "yyyyww") {
var week = getWeek(date);
if (week < 10) {
week = "0" + week;
}
return yearstr + "年" + week + "周";
}
else {
return yearstr + "年" + monthstr + "月" + daystr + "日";
}
}
时间字符串转换
var dateStr="2015-12-15";
console.log(dateStr.replace(/-/g,"/"));// 2015/12/15
console.log(now);
// Sun Apr 08 2018 14:38:48 GMT+0800 (中国标准时间)
console.log(now.getDate());
// 当前日 8
console.log(now.getMonth()+1);
//当前月 4
console.log(now.getFullYear());
//当前年 2018
console.log(now.getTime());
//当前时间戳(1970年到现在) 1523169789892
console.log(Date.parse("2018 05 12"));
console.log(Date.parse("05 12 2018"));
console.log(Date.parse("May 12,2018"));
console.log(Date.parse("2018,05,12"));
console.log(Date.parse("2018/05/12"));
//1970/01/01-2018/05/12的毫秒数 1526054400000
console.log(now.valueOf());
//返回当前时间的原始值,相当于上边的时间戳 1523169789892
var now=new Date();console.log(now.setDate(1));
//设置时间为本月的第一天,返回时间戳
//同样可以设置月,年等
console.log()
Date的构造函数
var date=new Date();
var date=new Date(string);//参照上边的时间戳格式
var date=new Date(year,month-1,day,hours,minutes,seconds);
var date=new Date(时间戳);
相关方法整理
//将date转换为特定的时间格式,可拓展
function dateToString(date, format) {
var yearstr = date.getFullYear();
var imonth = date.getMonth() + 1;
var monthstr = imonth + "";
if (imonth < 10) {
monthstr = "0" + monthstr;
}
var daystr = date.getDate();
if (daystr < 10) {
daystr = "0" + daystr;
}
if (format == "yyyymm") {
return yearstr + "年" + monthstr + "月";
}
else if (format == "yyyyss") {
return yearstr + "年" + parseInt(imonth / 3 + 1) + "季度";
}
else if (format == "yyyy") {
return yearstr + "年";
}
else if (format == "yyyyhh") {
return yearstr + "年" + (imonth > 6 ? "下半年" : "上半年");
}
else if (format == "yyyyww") {
var week = getWeek(date);
if (week < 10) {
week = "0" + week;
}
return yearstr + "年" + week + "周";
}
else {
return yearstr + "年" + monthstr + "月" + daystr + "日";
}
}
时间字符串转换
var dateStr="2015-12-15";
console.log(dateStr.replace(/-/g,"/"));// 2015/12/15