您的位置:首页 > Web前端 > JavaScript

JavaScript Date函数

2020-08-26 15:35 696 查看

文章目录

Date函数

月份:0-11月

new Date(参数)

0:当前时间
整数/多个整数之间使用逗号隔开:设置当前时间
new Date(2017,06,08)
new Date(2017,6,8)
new Date(2017,6,8,8,9)
字符串:
new Date("2017/06/06");
new Date("2017-06-06");
毫秒形式:

将一个字符串转换为Date对象的写法:
var str = "2012-12-12";
var date = new Date(str);  		//字符串转换为Date对象
document.write(date.getFullYear());	//然后就可以使用Date对象的方法输出年份了
Date.getDate()
返回是日期对象中月份中的几号。
var date = new Date();  		//2012-12-19
document.write(date.getDate());	//返回  19 是19号
Date.getDay()  
返回日期中的星期几  星期天0-星期6
var date = new Date();
document.write(date.getDay());	//3 星期3

Date.getFulYead()  
返回年份  如2012。
var date = new Date();
document.write(date.getFullYear());  //返回2012,2012年
Date.getHours()  
返回日期中的小时,几点了,0-23
var date = new Date();
document.write(date.getHours());  //返回23,晚上11点
Date.getMilliseconds()  
返回日期中的毫秒数
var date = new Date();
document.write(date.getMilliseconds());  //返回27  当前是xx年,xx月,xx点,xx分,xx秒,xx毫秒的毫秒

Date.getMinutes()    
返回日期中的分钟数  0-59
var date = new Date();
document.write(date.getMinutes());  //2012-12-19 23:22  返回22,12点22分

Date.getMonth()   
   返回日期中的月份数,返回值0(1月)-11(12月)
var date = new Date();
document.write(date.getMonth());  //2012-12-19  此处返回11,注意此处与通常理解有些偏差,1月份返回是0,12月返回是11

Date.getSeconds()    
返回一个日期的描述
var date = new Date();
document.write(date.getSeconds());·//返回34,2012-12-19 23:27:34  27分34秒

Date.getTime()      
将一个日期对象以毫秒形式返回
var date = new Date();
document.write(date.getTime());  //返回1355930928466  返回值是1970-01-01 午夜到当前时间的毫秒数。

Date.getTimezoneOffset()   
GMT时间与本地时间差,用分钟表示
var date = new Date();
document.write(date.getTimezoneOffset());  //返回-480  实际上这个函数获取的是javascript运行于哪个时区。单位是分钟。

Date.getUTCDate()    
返回Date对象中的日期值,(全球时间)
var date = new Date();
document.write(date.getUTCDate());  //返回19  19号

Date.getUTCDay()    
返回Date对象中的星期几,(全球时间)
var date = new Date();
document.write(date.getUTCDay());  //返回3  星期3

Date.getUTCFullYear()  
返回Date中的年份,4位,如2012,(全球时间)
var date = new Date();
document.write(date.getUTCFullYear());  //返回2012

Date.getUTCHours()  
返回Date对象中的小时数,就是现在是几点,终于有一个跟getHours()不同了,应该是时差关系,返回的是全球时间里的。
var date = new Date();
document.write(date.getUTCHours());  //现在北京时间是2012-12-19 23:44,但是返回的是15,也就是全球时间中的小时数。

Date.getUTCMilliserconds()  
返回Date对象中的毫秒数,(全球时间)
var date = new Date();
document.write(date.getMilliseconds());  //返回全球时间中的毫秒数

Date.getUTCMinutes()    
返回Date对象中的分钟数,(全球时间)
var date = new Date();
document.write(date.getMinutes());  //2012-12-19 23:49  返回49,注意是全球时间,其实全球时间应该就小时不同而已吧。

Date.getUTCMonth()    
返回Date对象中月份值,(全球时间)
var date = new Date();
document.write(date.getMonth());  //2012-12-19  返回11,0(1月份)-11(12月份)  

Date.getUTCSeconds()    
返回Date对象中的秒数值
var date = new Date();
document.write(date.getSeconds());  //返回秒数值 返回33

Date.getYear()    
返回Date对象中的年份值减去1900
var date = new Date();
document.write(date.getYear());  //2012-12-19  返回112 (2012-1900)

Date.now()    
静态方法  //返回1970-01-01午夜到现在的时间间隔,用毫秒表述

document.write(Date.now());  //静态方法,返回当前时间与1970-01-01的时间间隔,毫秒单位。

Date.parse()    
解析一个日期时间字符串,返回1970-01-01午夜到给定日期之间的毫秒数
var date = "2012-12-19";
document.write(Date.parse(date));  //返回  1355875200000
var da = new Date(date);
document.write("<br/>" + da.getFullYear() + "-" + da.getMonth() + "-" + da.getDate());  //输出2012-11-19  //注意月份是从0-11
Date.setDate()  
设置一个Date对象中的日期值,返回值用调整后的日期的毫秒表示
var date = new Date();
document.write(date.setDate(11));  

var da = new Date(date);
document.write("
" + da.getFullYear() + “-” + da.getMonth() + “-” + da.getDate()); //输出2012-11-11  //注意月份是从0-11,设置的时候要注意

Date.setFullYear()  
设置一个Date对象中的年份,返回值用调整后的日期的毫秒表示。
var date = new Date();  今天是2012-12-20
document.write(date.setFullYear(1989)); //返回630167981030
var da = new Date(date);
document.write("<br/>" + da.getFullYear() + "-" + da.getMonth() + "-" + da.getDate()); //输出1989-11-20

Date.setHours()  /
设置一个Date对象中的小事数,返回值用调整后的日期的毫秒表示。
var date = new Date();      //现在是2012-12-52 22:52
document.write(date.setHours(5)); //返回1355954000882
var da = new Date(date);
document.write("<br/>" + da.getHours()); //输出05
Date.setMilliseconds()  
设置一个日期的毫秒数
var date = new Date();      //现在是2012-12-20
document.write(date.setMilliseconds(22)); //返回1356015393022    注意最后两位,无论如何刷新都是22
Date.setMinutes()    
设置一个日期的分钟数
var date = new Date();      //现在是2012-12-52 22:52
document.write(date.setMinutes(1)); //返回1356012067105
var da = new Date(date);
document.write("<br/>" + da.getMinutes()); //输出1
Date.setMonth()      
设置一个日期的月份数
var date = new Date();      //现在是2012-12-20
document.write(date.setMonth(2)); //返回1332255597722
var da = new Date(date);
document.write("<br/>" + da.getMonth()); //输出2
Date.setSeconds()      
设置一个日期的描述

语法:
date.setSeconds(seconds)
      date.setSeconds(seconds,millis)
var date = new Date(); //现在是2012-12-20
document.write(date.setSeconds(3)); //返回1356015783872
var da = new Date(date);
document.write("
" + da.getSeconds()); //输出3

Date.setTime()        
使用毫秒数设置一个时间
var date = new Date();      //现在是2012-12-20
document.write(date.setTime(1356015783872)); //返回1356015783872
var da = new Date(date);
document.write("<br/>" + da.getDate()); //输出20
Date.setUTCDate()        
设置一个Date对象中对应月的日期值,就是几号(全球时间)

语法:
date.setUTCDate(day-of-month)
var date = new Date(); //现在是2012-12-20
document.write(date.setUTCDate(12)); //返回1355324952003
var da = new Date(date);
document.write("
" + da.getDate()); //输出12

Date.setUTCFullYear()     
设置一个Date对象中对应的年份,全球时间

语法:
date.setUTCFullYear(year)
      date.setUTCFullYear(year,month)
    date.setUTCFullYear(year,month,day)
var date = new Date();
document.write(date.setUTCFullYear(1999));
var da = new Date(date);
document.write("
" + da.getFullYear()); //输出1999

Date.setUTCHours()      
设置一个Date对象中对应的小时数,(全球时间)

语法:
date.setUTCHours(hours)
       date.setUTCHours(hours,minutes)
        date.setUTCHours(hours,minutes,seconds)
        date.setUTCHours(hours,minutes,seconds,millis)
var date = new Date();
document.write(date.setUTCHours(05));
var da = new Date(date);
document.write("
" + da.getUTCHours());

Date.setUTCMilliseconds()  
设置一个Date对象中对应的毫秒数,(全球时间)
var date = new Date();
document.write(date.setMilliseconds(05)); //注意此处无论如何刷新都是05结尾

Date.setUTCMinutes()    
设置一个Date对象的分钟、秒钟、以及毫秒值。

语法:
date.setUTCMinutes(minutes)
date.setUTCMinutes(minutes,seconds)
date.setUTCMinutes(minutes,seconds,millis)
var date = new Date(); //现在是2012-12-20
document.write(date.setUTCMinutes(25)); //返回1356017146549
var da = new Date(date);
document.write("
" + da.getUTCMinutes()); //输出5

Date.setUTCMonth()    
设置一个Date对象的月份值及日期值
var date = new Date();				//现在是2012-12-20
document.write(date.setMonth(01));	//返回1329751527983
var da = new Date(date);
document.write("<br/>" + da.getUTCMonth()); //输出1

Date.setUTCSeconds()  
设置一个Date的秒钟及毫秒值
var date = new Date();					//现在是2012-12-20
document.write(date.setUTCSeconds(01)); //返回1356017281976
var da = new Date(date);
document.write("<br/>" + da.getUTCSeconds()); //输出1

Date.setYears()      
设置一个Date对象的年份值,如果给的参数在0-99之间,它将会加上1900以便把它当中1900-1999之间的年份处理。如果输入4位数,则把它当成FullYear设置
var date = new Date();				//现在是2012-12-20
document.write(date.setYear(22));	//返回1356017281976
var da = new Date(date);
document.write("<br/>" + da.getFullYear()); //输出1922

var date = new Date();				//现在是2012-12-20
document.write(date.setYear(2011)); //返回1324395113386
var da = new Date(date);
document.write("<br/>" + da.getFullYear()); //输出2011

Date.toDateString()    
以字符串的形式返回一个Date的日期部分
var date = new Date();
document.write(date.toDateString("yyyy-MM-dd"));

Date.toTimeString()    
以字符串的形式返回一个Date的时间部分
var date = new Date();
document.write(date.toTimeString("yyyy-MM-dd"));

Date.toISOString()    
将一个Date对象转换为ISO-8601格式的字符串,返回的字符串格式为yyyy-mm-ddThh:mm:ssZ
var date = new Date();
document.write(date.toISOString());

Date.toJSON       
//JSON序列化一个对象
var date = new Date();
document.write(date.toJSON());

Date.toLocaleDateString()  
以本地格式的字符串返回一个Date的日期部分,返回一个本地人可读的日期格式,日期部分
var date = new Date();
document.write(date.toLocaleDateString());

Date.toLocaleString()    
将一个Date转化难为一个本地格式的字符串
var date = new Date();
document.write(date.toLocaleString());

Date.toLocaleTimeString()    
将一个Date转化为本地的格式的时间部分
var date = new Date();
document.write(date.toLocaleTimeString());

Date.toString()          
将一个Date转换为一个字符串
var date = new Date();			//现在是2012-12-22
document.write(date.toString());//返回Sat Dec 22 2012 19:59:17 GMT+0800

Date.toTimeString()       
以字符串的形式返回一个Date对象的时间部分
var date = new Date();
document.write(date.toString());
Date.toUTCString()       
将一个Date对象转换为字符串(全球时间)
var date = new Date();
document.write(date.toUTCString());

Date.UTC()           
将一个Date对象转换毫秒的形式  静态方法
语法:Date.UTC(year,month,day,hours,minutes,seconds,ms)

document.write(Date.UTC(2011, 11, 11, 11, 11, 11));

Date.valueOf()         
如果是一个Date对象,将一个Date对象转为毫秒的形式,否则不显示
var date = "";
document.write(date.valueOf());    //不是Date对象,不输出
var date1 = new Date();
document.write(date1.valueOf());   //输出1356180400916
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: