Summarizing the use of date objects in JavaScript _javascript tips

Source: Internet
Author: User
Tags current time getdate string format time interval tojson


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



/date (1379944571737)/



But this date does not appear directly, because no one knows what it means, and here's a way to turn the JSON date to a JS date.


function Convertjsondatetojsdate (jsondate) {
 var date = new Date (parseint Jsondate.replace ("/date (", ""). Replace ( ")/", "" "));
 return date;
}


In the date format that provides two dates to become accustomed to vision:


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;
}


Converts a string to the Date object's wording:


 var str = "2012-12-12";
 var date = new Date (str);  The string is converted to the Date object
 document.write (Date.getfullyear ()); You can then use the method of the date object to output the year.


One, Date.getdate () returns the number of the month in the Date object.


 var date = new Date ();  2012-12-19
 document.write (Date.getdate ()); Return 19 is number 19th.


Ii. Date.getday () Return date of the week of Sunday 0-Week 6


 var date = new Date ();
 document.write (Date.getday ()); 3 weeks 3


Third, Date.getfulyead () returns the year as 2012.


 var date = new Date ();
 document.write (Date.getfullyear ()); Back to 2012,2012 years


Four, date.gethours () returns the hour of the date, a few points, 0-23


 var date = new Date ();
 document.write (Date.gethours ()); Return 23, 11 o'clock in the evening


V. Date.getmilliseconds () The number of milliseconds in the return date


 var date = new Date ();
 document.write (Date.getmilliseconds ()); Return 27 The current is XX years, XX month, xx point, xx, xx seconds, xx millisecond milliseconds


Vi. date.getminutes () returns the number of minutes in the date 0-59


 var date = new Date ();
 document.write (Date.getminutes ()); 2012-12-19 23:22 return 22, 12:22


Seven, Date.getmonth ()//Returns the number of months in the date, return value 0 (January)-11 (December)


 var date = new Date ();
 document.write (Date.getmonth ()); 2012-12-19 here returns 11, note that there are some deviations from the usual understanding, January return is 0, December return is 11


Viii. date.getseconds ()//Return a description of a date


 var date = new Date ();
 document.write (Date.getseconds ()); · Back to 34,2012-12-19 23:27:34 27 minutes 34 seconds


Nine, Date.gettime ()//Return a Date object in milliseconds


 var date = new Date ();
 document.write (Date.gettime ()); The return 1355930928466 return value is the number of milliseconds from midnight 1970-01-01 to the current time.


Ten, Date.gettimezoneoffset ()//gmt time and local difference, expressed in minutes


 var date = new Date ();
 document.write (Date.gettimezoneoffset ()); Return-480 Actually this function gets the time zone in which JavaScript is running. Unit is minutes.


Xi. date.getutcdate ()//returns the date value in the Date object (global time)


 var date = new Date ();
 document.write (Date.getutcdate ()); return 19, 19th.


12, Date.getutcday ()//Returns the day of the week in the Date object (global time)


 var date = new Date ();
 document.write (Date.getutcday ()); Back 3 weeks 3


13, Date.getutcfullyear ()//returns the year in Date, 4 bits, such as 2012, (Global Time)


 var date = new Date ();
 document.write (Date.getutcfullyear ()); Return 2012


14, Date.getutchours ()//Returns the number of hours in the Date object, which is now the time, finally there is a difference with gethours (), it should be jet lag, the return of the global time.


 var date = new Date ();
 document.write (Date.getutchours ()); Now Beijing time is 2012-12-19 23:44, but the return is 15, that is, the number of hours in global time.


XV, Date.getutcmilliserconds ()//Returns the number of milliseconds in the Date object (global time)


 var date = new Date ();
 document.write (Date.getmilliseconds ()); Returns the number of milliseconds in global time


16, Date.getutcminutes ()//Returns the number of minutes in the Date object (global time)


 var date = new Date ();
 document.write (Date.getminutes ()); 2012-12-19 23:49 return 49, attention is global time, in fact, the global time should be different from the hour.


17, Date.getutcmonth ()//Returns the value of the month in the Date object (global time)


 var date = new Date ();
 document.write (Date.getmonth ()); 2012-12-19 return to 11,0 (January)-11 (December)


18, Date.getutcseconds ()//returns the number of seconds in the Date object


 var date = new Date ();
 document.write (Date.getseconds ()); Return seconds value returns 33


19, Date.getyear ()//returns the year value in the Date object minus 1900


 var date = new Date ();
 document.write (Date.getyear ()); 2012-12-19 return 112 (2012-1900)


20, Date.now () static method//Return 1970-01-01 midnight to the current time interval, expressed in milliseconds


document.write (Date.now ()); static method, which returns the current time with the time interval of 1970-01-01, the millisecond unit.


21, Date.parse ()//parsing a date-time string, returns the number of milliseconds between midnight 1970-01-01 and the given date


 var date = "2012-12-19";
 document.write (Date.parse (Date)); Returns 1355875200000
 var da = new Date (date);
 document.write ("<br/>" + da.getfullyear () + "-" + da.getmonth () + "-" + da.getdate ()); Output 2012-11-19//Note month is from 0-11


22, Date.setdate ()//Set the date value in a Date object, the return value with the adjusted date of the millisecond representation


 var date = new Date ();
 document.write (Date.setdate (11)); return 1355236647980//set to 11, actually December, set to 3 is actually April
var da = new Date (date);
 document.write ("<br/>" + da.getfullyear () + "-" + da.getmonth () + "-" + da.getdate ()); Output 2012-11-11//Note the month is from 0-11, set the time to note


23, Date.setfullyear ()//Setting the year in a Date object, the return value is represented by the millisecond of the adjusted date.


 var date = new Date (); Today is 2012-12-20
 document.write (Date.setfullyear (1989));//return 630167981030
 var da = new Date (date);
 document.write ("<br/>" + da.getfullyear () + "-" + da.getmonth () + "-" + da.getdate ()); Output 1989-11-20


24, Date.sethours ()//Set the number of trivial things in a Date object, the return value is expressed in milliseconds of the adjusted date.


 var date = new Date (); It is now 2012-12-52 22:52
 document.write (date.sethours (5));//return 1355954000882
 var da = new Date (date);
 document.write ("<br/>" + da.gethours ()); Output 05


25, Date.setmilliseconds ()//Set the number of milliseconds for a date


 var date = new Date (); It is now 2012-12-20
 document.write (Date.setmilliseconds (22));//return 1356015393022 note the last two digits, regardless of the refresh is 22


26, Date.setminutes ()//Set the number of minutes of a date


 var date = new Date (); It is now 2012-12-52 22:52
 document.write (date.setminutes (1));//return 1356012067105
 var da = new Date (date);
 document.write ("<br/>" + da.getminutes ()); Output 1


27, Date.setmonth ()//Set the number of months of a date


 var date = new Date (); It is now 2012-12-20
 document.write (Date.setmonth (2));//return 1332255597722
 var da = new Date (date);
 document.write ("<br/>" + da.getmonth ()); Output 2


28, Date.setseconds ()//Set a description of the date



Syntax: date.setseconds (seconds)



Date.setseconds (Seconds,millis)


 var date = new Date (); It is now 2012-12-20
 document.write (Date.setseconds (3));//return 1356015783872
 var da = new Date (date);
 document.write ("<br/>" + da.getseconds ()); Output 3


29, Date.settime ()//use milliseconds to set a time



Syntax: Date.settime (milliseonds)


 var date = new Date (); It is now 2012-12-20
 document.write (Date.settime (1356015783872));//return 1356015783872
 var da = new Date (date);
 document.write ("<br/>" + da.getdate ()); Output 20


30, Date.setutcdate ()//Set the date value of the corresponding month in a Date object, which is the number (global time)



Syntax: Date.setutcdate (Day-of-month)


 var date = new Date (); It is now 2012-12-20
 document.write (date.setutcdate (12));//return 1355324952003
 var da = new Date (date);
 document.write ("<br/>" + da.getdate ()); Output 12


31, Date.setutcfullyear ()//set the corresponding year in a Date object, global time



Syntax: Date.setutcfullyear (year)



Date.setutcfullyear (Year,month)



Date.setutcfullyear (Year,month,day)


 var date = new Date (); It is now 2012-12-20
 document.write (Date.setutcfullyear (1999));//return 945702713666
 var da = new Date (date);
 


32, Date.setutchours ()//set the corresponding number of hours in a Date object (global time)



Syntax: date.setutchours (Hours)



Date.setutchours (Hours,minutes)



Date.setutchours (Hours,minutes,seconds)



Date.setutchours (Hours,minutes,seconds,millis)


 var date = new Date (); It is now 2012-12-20
 document.write (date.setutchours (05));//return 1355980581928
 var da = new Date (date);
 


33, Date.setutcmilliseconds ()//set the corresponding number of milliseconds in a Date object (global time)



Syntax: Date.setutcmilliseconds (Millis)


  var date = new Date (); It is now 2012-12-20
  document.write (date.setmilliseconds (05));//return 1356016784005 Note that the refresh is 05 at the end anyway.


34, Date.setutcminutes ()//Set the minute, second, and millisecond values of a Date object.



Syntax: date.setutcminutes (minutes)



Date.setutcminutes (Minutes,seconds)



Date.setutcminutes (Minutes,seconds,millis)


 var date = new Date (); It is now 2012-12-20
 document.write (Date.setutcminutes (25));//return 1356017146549
 var da = new Date (date);
 


35, Date.setutcmonth ()//Set the month value and date value of a Date object



Syntax: Date.setutcmonth (month)



Date.setutcmonth (Month,day)


 var date = new Date (); It is now 2012-12-20
 document.write (date.setmonth (01));//return 1329751527983
 var da = new Date (date);
 


36, Date.setutcseconds ()//Set a Date of the second and the millisecond value



Syntax: date.setutcseconds (seconds)



Date.setutcseconds (Seconds,millis)


 var date = new Date (); It is now 2012-12-20
 document.write (date.setutcseconds (01));//return 1356017281976
 var da = new Date (date);
 


37. Date.setyears ()//sets the year value of a Date object, and if the given parameter is between 0-99, it will add 1900 to handle the year between 1900-1999 of it. If you enter a 4-digit number, consider it a fullyear setting.



Syntax: Date.setyears (year)


 var date = new Date (); It is now 2012-12-20
 document.write (Date.setyear (22));//return 1356017281976
 var da = new Date (date);
 document.write ("<br/>" + da.getfullyear ()); Output 1922
 var date = new Date ();//Now 2012-12-20
 document.write (Date.setyear (2011));//Back to 1324395113386
 var da = new Date (date);
 document.write ("<br/>" + da.getfullyear ()); Output 2011


38, Date.todatestring ()//Returns a date part of the form as a string



Syntax: date.todatestring ()


 var date = new Date (); It's 2012-12-20.
 


39, Date.totimestring ()//Returns the time part of a date as a string



Syntax: date.totimestring ()


 var date = new Date (); It's 2012-12-20.
 


40, date.toisostring ()//Converting a Date object to a ISO-8601 format string



syntax; date.toisostring ()//return string format is YYYY-MM-DDTHH:MM:SSZ


 var date = new Date (); It's 2012-12-20.
 


41, Date.tojson//json serialization of an object



Syntax: A string representation of Date.tojson (key)//date, whose value is the result of the toisostring () method that invokes it


 var date = new Date (); It's 2012-12-20.
 


42, Date.tolocaledatestring ()//Returns a date part of a day in a locally formatted string



Syntax: date.tololcaledatestring//Return to a native readable date format, date part


 var date = new Date (); It is now 2012-12-20
 document.write (date.tolocaledatestring ());//Return December 20, 2012


43. Date.tolocalestring ()//Convert a Date to a local format string



Syntax: date.tolocalestring ()


 var date = new Date (); It's 2012-12-22.
 


44. Date.tolocaletimestring ()//Time part of converting a Date into a local format


 var date = new Date (); It's 2012-12-22.
 


45, Date.tostring ()//Convert a Date to a string


 var date = new Date (); It's 2012-12-22.
 


46, Date.totimestring ()//Returns the time portion of a Date object as a string


 var date = new Date (); It's 2012-12-22.
 


47. Date.toutcstring ()//Convert a Date object to a string (global time)


 var date = new Date (); It's 2012-12-22.
 


48, DATE.UTC ()//The form static method of converting a Date object to milliseconds



Syntax: DATE.UTC (YEAR,MONTH,DAY,HOURS,MINUTES,SECONDS,MS)



    document.write(Date.UTC(2011, 11, 11, 11, 11, 11)); //return 1323601871000



49, Date.valueof ()///If it is a Date object, converts a Date object to milliseconds, otherwise it does not display


 var date = "";
 document.write (Date.valueof ()); is not a Date object and does not output
 var date1 = new Date ();
 


The above is the entire content of this article, thanks to the cloud Habitat Community support!


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.