Home > Web Front-end > JS Tutorial > Summary of the use of Date objects in javascript

Summary of the use of Date objects in javascript

高洛峰
Release: 2016-12-06 10:20:33
Original
1171 people have browsed it

JSON date to JS date, we know that after the date type is converted to JSON, the returned data is similar to this:

  /Date(1379944571737)/

 But this kind of date cannot be displayed directly because no one knows what it is. Meaning, here is a way to convert JSON date to JS date.

function ConvertJSONDateToJSDate(jsondate) {
 var date = new Date(parseInt(jsondate.replace("/Date(", "").replace(")/", ""), 10));
 return date;
}
Copy after login


provides two date formats for Date conversion:

//yyyy-MM-dd
function getDate(date) {
 var year = date.getFullYear();
 var month = date.getMonth() + 1;
 var day = date.getDate();
 return year + "-" + month + "-" + day ;
}
//yyyy-MM-dd HH:mm:SS
function getDateTime(date) {
 var year = date.getFullYear();
 var month = date.getMonth() + 1;
 var day = date.getDate();
 var hh = date.getHours();
 var mm = date.getMinutes();
 var ss = date.getSeconds();
 return year + "-" + month + "-" + day + " " + hh + ":" + mm + ":" + ss;
}
Copy after login


How to convert a string into a Date object:

var str = "2012-12-12";
var date = new Date(str);  //字符串转换为Date对象
document.write(date.getFullYear());  //然后就可以使用Date对象的方法输出年份了
Copy after login


1.Date .getDate() Returns the day of the month in the date object.

var date = new Date();  //2012-12-19
document.write(date.getDate());  //返回 19 是19号
Copy after login


2. Date.getDay() Returns the day of the week in the date Sunday 0-6

var date = new Date();
document.write(date.getDay());  //3 星期3
Copy after login


3. Date.getFulYead() Returns the year such as 2012.

var date = new Date();
document.write(date.getFullYear());  //返回2012,2012年
Copy after login


4. Date.getHours() Returns the hours in the date, what time it is, 0-23

var date = new Date();
document.write(date.getHours());  //返回23,晚上11点
Copy after login


5. Date.getMilliseconds() Returns the milliseconds in the date

var date = new Date();
document.write(date.getMilliseconds());  //返回27  当前是xx年,xx月,xx点,xx分,xx秒,xx毫秒的毫秒
Copy after login


6. Date.getMinutes()                     

 

                        vi. (January)-11(December)

var date = new Date();
document.write(date.getMinutes());  //2012-12-19 23:22  返回22,12点22分
Copy after login

Eight. Date.getSeconds() //Returns a description of a date

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

Nine. Date.getTime() //Will A date object returns

var date = new Date();
document.write(date.getSeconds());·//返回34,2012-12-19 23:27:34  27分34秒
Copy after login


, date.gettimezoneoffset () // gmt time is different from the local time. / Return the date value in the Date object, (global time)

var date = new Date();
document.write(date.getTime());  //返回1355930928466  返回值是1970-01-01 午夜到当前时间的毫秒数。
Copy after login

12. Date.getUTCDay() // Return the day of the week in the Date object, (global time)

var date = new Date();
document.write(date.getTimezoneOffset());  //返回-480  实际上这个函数获取的是javascript运行于哪个时区。单位是分钟。
Copy after login

Thirteen. Date.getUTCFulYear() // Returns the year in Date, 4 digits, such as 2012, (global time)

var date = new Date();
document.write(date.getUTCDate());  //返回19  19号
Copy after login

Fourteen. Date.getUTCHours() // Returns the year in Date object The number of hours, that is, what time it is now, finally has one that is different from getHours(). It should be related to the time difference, and the returned value is in global time.

var date = new Date();
document.write(date.getUTCDay());  //返回3  星期3
Copy after login

Fifteen, Date.getUTCMillisconds() //Returns the number of milliseconds in the Date object, (global time)

var date = new Date();
document.write(date.getUTCFullYear());  //返回2012
Copy after login

Sixteen, Date.getUTCMinutes() //Returns D ate The number of minutes in the object, (global time)

var date = new Date();
document.write(date.getUTCHours());  //现在北京时间是2012-12-19 23:44,但是返回的是15,也就是全球时间中的小时数。
Copy after login

Seventeen, Date.getUTCMonth() //Returns the month value in the Date object, (global time)

var date = new Date();
document.write(date.getMilliseconds());  //返回全球时间中的毫秒数
Copy after login

Eighteen, Date.getUTCSeconds()  //Returns the seconds value in the Date object

var date = new Date();
document.write(date.getMinutes());  //2012-12-19 23:49  返回49,注意是全球时间,其实全球时间应该就小时不同而已吧。
Copy after login

Nineteen, Date.getYear()  //Returns the year value in the Date object minus 1900

var date = new Date();
document.write(date.getMonth());  //2012-12-19  返回11,0(1月份)-11(12月份)  
Copy after login

20. Date.now() Static method // Returns the time interval from midnight on January 1, 1970 to now, expressed in milliseconds

var date = new Date();
document.write(date.getSeconds());  //返回秒数值 返回33
Copy after login

21. Date.parse() // Parse a date Time string, returns the number of milliseconds between midnight on January 1, 1970 and the given date

var date = new Date();
document.write(date.getYear());  //2012-12-19  返回112 (2012-1900)
Copy after login

Twenty-two, Date.setDate() //Set the date value in a Date object, return value Expressed in milliseconds of the adjusted date

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

23. Date.setFullYear() // Set the year in a Date object, and the return value is expressed in milliseconds of the adjusted date.

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
Copy after login

Twenty-four, Date.setHours() //Set the number of events in a Date object, and the return value is expressed in milliseconds of the adjusted date.

var date = new Date();
document.write(date.setDate(11));  //返回1355236647980    //设置为11,其实是12月,设置为3其实是4月
    var da = new Date(date);
document.write("<br/>" + da.getFullYear() + "-" + da.getMonth() + "-" + da.getDate()); //输出2012-11-11  //注意月份是从0-11,设置的时候要注意
Copy after login

Twenty-five, Date.setMilliseconds() //Set the number of milliseconds on a 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
Copy after login

Twenty-six, Date.setMinutes() //Set the number of minutes on a 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
Copy after login

Twenty-seven, Date.setMonth() //Set the number of months for a date

var date = new Date(); //现在是2012-12-20
document.write(date.setMilliseconds(22)); //返回1356015393022    注意最后两位,无论如何刷新都是22
Copy after login

Twenty-eight, Date.setSeconds() //Set the description syntax of a date :date.setSeconds(seconds) Date.setSeconds(seconds,millis)

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
Copy after login

Twenty-nine, Date.setTime() //Set a time using milliseconds Syntax: date.setTime(milliseonds)

var date = new Date(); //现在是2012-12-20
document.write(date.setMonth(2)); //返回1332255597722
var da = new Date(date);
document.write("<br/>" + da.getMonth()); //输出2
Copy after login

Thirty, Date.setUTCDate() //Set the date value corresponding to the month in a Date object, which is the day (global time) Syntax: date.setUTCDate(day-of-month)

var date = new Date(); //现在是2012-12-20
document.write(date.setSeconds(3)); //返回1356015783872
var da = new Date(date);
document.write("<br/>" + da.getSeconds()); //输出3
Copy after login

三十一、Date.setUTCFullYear()     //设置一个Date对象中对应的年份,全球时间      语法:date.setUTCFullYear(year)         date.setUTCFullYear(year,month)         date.setUTCFullYear(year,month,day)

var date = new Date(); //现在是2012-12-20
document.write(date.setUTCFullYear(1999)); //返回945702713666
var da = new Date(date);
document.write("<br/>" + da.getFullYear()); //输出1999
Copy after login


三十二、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(); //现在是2012-12-20
document.write(date.setUTCHours(05)); //返回1355980581928
var da = new Date(date);
document.write("<br/>" + da.getUTCHours()); //输出5
Copy after login


三十三、Date.setUTCMilliseconds()  //设置一个Date对象中对应的毫秒数,(全球时间)     语法:date.setUTCMilliseconds(millis)

var date = new Date(); //现在是2012-12-20
document.write(date.setMilliseconds(05)); //返回1356016784005  注意此处无论如何刷新都是05结尾
Copy after login


三十四、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("<br/>" + da.getUTCMinutes()); //输出5
Copy after login


三十五、Date.setUTCMonth()    //设置一个Date对象的月份值及日期值       语法:date.setUTCMonth(month)          date.setUTCMonth(month,day)

var date = new Date(); //现在是2012-12-20
document.write(date.setMonth(01)); //返回1329751527983
var da = new Date(date);
document.write("<br/>" + da.getUTCMonth()); //输出1
Copy after login


三十六、Date.setUTCSeconds()   //设置一个Date的秒钟及毫秒值      语法:date.setUTCSeconds(seconds)         date.setUTCSeconds(seconds,millis)

var date = new Date(); //现在是2012-12-20
document.write(date.setUTCSeconds(01)); //返回1356017281976
var da = new Date(date);
document.write("<br/>" + da.getUTCSeconds()); //输出1
Copy after login


三十七、Date.setYears()      //设置一个Date对象的年份值,如果给的参数在0-99之间,它将会加上1900以便把它当中1900-1999之间的年份处理。如果输入4位数                   则把它当成FullYear设置      语法:date.setYears(year)

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
Copy after login


三十八、Date.toDateString()    //以字符串的形式返回一个Date的日期部分     语法:date.toDateString()

var date = new Date(); //现在是2012-12-20
document.write(date.toDateString("yyyy-MM-dd")); //返回Thu Dec 20 2012
Copy after login


三十九、Date.toTimeString()    //以字符串的形式返回一个Date的时间部分     语法:date.toTimeString()

var date = new Date(); //现在是2012-12-20
document.write(date.toTimeString("yyyy-MM-dd")); //返回23:38:33 GMT+0800
Copy after login


四十、Date.toISOString()     //将一个Date对象转换为ISO-8601格式的字符串     语法;date.toISOString()  //返回的字符串格式为yyyy-mm-ddThh:mm:ssZ

var date = new Date(); //现在是2012-12-20
document.write(date.toISOString()); //返回2012-12-20T15:45:47.493Z
Copy after login


四十一、Date.toJSON      //JSON序列化一个对象     语法:date.toJSON(key)  //date的一个字符串表示形式,值为调用它的toISOString()方法的结果

var date = new Date(); //现在是2012-12-20
document.write(date.toJSON()); //返回2012-12-20T15:45:47.493Z
Copy after login


四十二、Date.toLocaleDateString()  //以本地格式的字符串返回一个Date的日期部分语法:date.toLolcaleDateString  //返回一个本地人可读的日期格式,日期部分

var date = new Date(); //现在是2012-12-20
document.write(date.toLocaleDateString()); //返回2012年12月20日
Copy after login


四十三、Date.toLocaleString()    //将一个Date转化难为一个本地格式的字符串 语法:date.toLocaleString()

var date = new Date(); //现在是2012-12-22
document.write(date.toLocaleString()); //返回2012年12月22日 19:56:40
Copy after login


四十四、Date.toLocaleTimeString()    //将一个Date转化为本地的格式的时间部分

var date = new Date(); //现在是2012-12-22
document.write(date.toLocaleTimeString()); //返回19:57:23
Copy after login


四十五、Date.toString()          //将一个Date转换为一个字符串

var date = new Date(); //现在是2012-12-22
document.write(date.toString()); //返回Sat Dec 22 2012 19:59:17 GMT+0800
Copy after login
Copy after login


四十六、Date.toTimeString()       //以字符串的形式返回一个Date对象的时间部分

var date = new Date(); //现在是2012-12-22
document.write(date.toString()); //返回Sat Dec 22 2012 19:59:17 GMT+0800
Copy after login
Copy after login


四十七、Date.toUTCString()      //将一个Date对象转换为字符串(全球时间)

var date = new Date(); //现在是2012-12-22
document.write(date.toUTCString()); //返回Sat, 22 Dec 2012 12:00:42 GMT
Copy after login


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

document.write(Date.UTC(2011, 11, 11, 11, 11, 11)); //返回1323601871000
Copy after login


四十九、Date.valueOf() 

//如果是一个Date对象,将一个Date对象转为毫秒的形式,否则不显示

var date = "";
document.write(date.valueOf()); //不是Date对象,不输出
var date1 = new Date();
document.write(date1.valueOf()); //输出1356180400916
Copy after login

   



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template