示例一:
示例二:
document.writeln(date);
//Thu Jan 08 2015 01:25:53 GMT+0800 (中国标准时间)
document.writeln(Date.parse());
//NaN
document.writeln(Date.parse('6/10/2014'));
//1402329600000 毫秒数
document.writeln(Date.parse('Thu Jan 08 2015 01:25:53'));
//1420651553000 毫秒数
document.writeln(Date.UTC());
//NaN
//日期格式化<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>
document.writeln('<br/>');
document.writeln(date.toDateString());
//Thu Jan 08 2015
document.writeln('<br/>')
document.writeln(date.toTimeString());
//01:39:08 GMT+0800 (中国标准时间)
document.writeln('<br/>')
document.writeln(date.toLocaleDateString());
//2015年1月8日
document.writeln('<br/>')
document.writeln(date.toLocaleTimeString());
//上午1:39:08
document.writeln('<br/>')
document.writeln(date.toUTCString());
//Wed,07 Jan 2015 17:39:08 GMT
//一些set,get<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>
示例三:
示例四:
var MMMArr=["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"];
var WeekArrEn=["Sun","Mon","Tue","Web","Thu","Fri","Sat"];
var WeekArr=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
var oNumber=time.Millisecond/1000;
if(format!=undefined && format.replace(/\s/g,"").length>0){
if(loc!=undefined && loc =="en"){
MMMArr=MMMArrEn.slice(0);
WeekArr=WeekArrEn.slice(0);
}
format=format
.replace(/yyyy/ig,time.Year)
.replace(/yyy/ig,time.Year)
.replace(/yy/ig,time.TYear)
.replace(/y/ig,time.TYear)
.replace(/MMM/g,MMMArr[time.Month-1])
.replace(/MM/g,time.TMonth)
.replace(/M/g,time.Month)
.replace(/dd/ig,time.TDay)
.replace(/d/ig,time.Day)
.replace(/HH/g,time.THour)
.replace(/H/g,time.Hour)
.replace(/hh/g,time.Thour)
.replace(/h/g,time.hour)
.replace(/mm/g,time.TMinute)
.replace(/m/g,time.Minute)
.replace(/ss/ig,time.TSecond)
.replace(/s/ig,time.Second)
.replace(/fff/ig,time.Millisecond)
.replace(/ff/ig,oNumber.toFixed(2)100)
.replace(/f/ig,oNumber.toFixed(1)10)
.replace(/EEE/g,WeekArr[time.Week]);
}
else{
format=time.Year+"-"+time.Month+"-"+time.Day+" "+time.Hour+":"+time.Minute+":"+time.Second;
}
return format;
}
var d=new Date();
console.log(d.toString()); //2014-7-27 9:26:52
console.log(d.toString("")); //2014-7-27 9:26:52
console.log(d.toString("yyyy-MM-dd HH:mm:ss")); //2014-07-27 09:26:52
console.log(d.toString("yyyy年MM月dd日 HH:mm:ss")); //2014年07月27日 09:26:52
console.log(d.toString("yyyy-MM-dd HH:mm:ss fff")); //2014-07-27 09:26:52 237
console.log(d.toString("yyyy年 MMM dd EEE")); //2014年 七月 27 星期五
console.log(d.toString("yyyy MMM dd EEE","en")); //2014 Jul 27 Fri
示例五:时间个性化输出功能
if(year < curYear){
timeStr = year +'年'+ month +'月'+ day +'日 '+ hour +':'+ minute;
}else{
var pastTime = curDate - date,pastH = pastTime/3600000;
if(pastH > curHour){
timeStr = month +'月'+ day +'日 '+ hour +':'+ minute;
}else if(pastH >= 1){
timeStr = '今天 ' + hour +':'+ minute +'分';
}else{
var pastM = curDate.getMinutes() - minute;
if(pastM > 1){
timeStr = pastM +'分钟前';
}else{
timeStr = '刚刚';
}
}
}
return timeStr;
}
以上所述就是本文的全部内容了,希望大家能够喜欢。
原文链接:https://www.f2er.com/js/53753.html