JavaScript Advanced Programming Reading notes ten local objects date dates _javascript Tips

Source: Internet
Author: User
Tags deprecated getdate local time
Create
var d=new Date ();
Note that the value of the month in JavaScript is from 0 to 11 (0 for January).

Set Date and time values
There are two ways to set date and time values:

1, only to declare the number of milliseconds from January 1, 1970 12 o'clock in the morning

A, directly with the number of milliseconds from January 1, 1970 12 o'clock in the morning

var d=new Date (0);
B, Parse Method:

The parse method takes a string as a parameter, converts the string to a date value, and returns the number of milliseconds.

For example, to create a Date object for February 27, 2012:

var d=new Date (Date.parse ("Feb 27,2012"));
If the string passed to the parse method cannot be converted to a date, the function returns Nan

C, UTC Method:

The UTC method also returns the millisecond representation of the date, but the parameters are year, month, day, time, minute, second, millisecond, year, month, and others are optional.

For example, to create a Date object for February 27, 2012:

var d=new Date (DATE.UTC (2012,1,27));
2, directly declare the parameters accepted by the UTC method

var d=new Date (2012,1,27);
Parameter rules are identical to the UTC method.

Date class method
The date class method is as follows (from: http://www.jb51.net/w3school/js/jsref_obj_date.htm):
Method Description FF IE
Date () Returns the date and time of the day. 1 3
GetDate () Returns one day of one months from the Date object (1 ~ 31). 1 3
Getday () Returns one day of the week from the Date object (0 ~ 6). 1 3
GetMonth () Returns the month from the date object (0 ~ 11). 1 3
getFullYear () Returns the year with a four-digit number from the Date object. 1 4
GetYear () Please use the getFullYear () method instead. 1 3
GetHours () Returns the hour of the date object (0 ~ 23). 1 3
Getminutes () Returns the Minutes (0 ~ 59) of the Date object. 1 3
Getseconds () Returns the number of seconds (0 ~ 59) of the Date object. 1 3
Getmilliseconds () Returns the millisecond (0 ~ 999) of the Date object. 1 4
GetTime () Returns the number of milliseconds since January 1, 1970. 1 3
getTimezoneOffset () Returns the minute difference between local time and Greenwich Mean Time (GMT). 1 3
getUTCDate () Returns the day of the month from the date object according to the world (1 ~ 31). 1 4
getUTCDay () Returns the day of the week from the Date object based on the world (0 ~ 6). 1 4
getUTCMonth () Returns the month (0 ~ 11) from the Date object according to the world. 1 4
getUTCFullYear () Returns a four-digit year from the Date object, based on the world. 1 4
getUTCHours () The hour (0 ~ 23) that returns the Date object according to the universal. 1 4
getUTCMinutes () Returns the minute (0 ~ 59) of the date object according to the universal. 1 4
getUTCSeconds () Returns the second (0 ~ 59) of the date object according to the universal. 1 4
getUTCMilliseconds () Returns the millisecond (0 ~ 999) of the date object according to the universal. 1 4
Parse () Returns the number of milliseconds from midnight January 1, 1970 to the specified date (string). 1 3
Setdate () Sets the day of the month in the Date object (1 ~ 31). 1 3
Setmonth () Sets the month in the Date object (0 ~ 11). 1 3
setFullYear () Sets the year (four digits) in the Date object. 1 4
Setyear () Please use the setFullYear () method instead. 1 3
Sethours () Sets the hour in the Date object (0 ~ 23). 1 3
Setminutes () Sets the minutes in the Date object (0 ~ 59). 1 3
Setseconds () Sets the seconds in the Date object (0 ~ 59). 1 3
Setmilliseconds () Sets the milliseconds in the Date object (0 ~ 999). 1 4
SetTime () Sets the Date object in milliseconds. 1 3
setUTCDate () Sets the day of the month in the Date object according to the world (1 ~ 31). 1 4
setUTCMonth () Sets the month in the Date object according to the world (0 ~ 11). 1 4
setUTCFullYear () Sets the year (four digits) in the Date object based on the world. 1 4
setUTCHours () Sets the hour in the Date object according to the world (0 ~ 23). 1 4
setUTCMinutes () Sets the minutes in the date object according to the world (0 ~ 59). 1 4
setUTCSeconds () Sets the second (0 ~ 59) in the Date object according to the world. 1 4
setUTCMilliseconds () Sets the milliseconds in the Date object according to the world (0 ~ 999). 1 4
Tosource () Returns the source code for this object. 1 -
ToString () Converts the Date object to a string. 1 4
toTimeString () Converts the time portion of a Date object to a string. 1 4
toDateString () Converts the date part of a Date object to a string. 1 4
toGMTString () Please use the toutcstring () method instead. 1 3
toUTCString () Converts a Date object to a string according to the universal. 1 4
toLocaleString () Converts the Date object to a string, based on the local time format. 1 3
toLocaleTimeString () Converts the time portion of a Date object to a string, based on the local time format. 1 3
toLocaleDateString () Converts the date part of a Date object to a string, based on the local time format. 1 3
UTC () Returns the number of milliseconds from January 1, 1997 to a specified date according to the Universal. 1 3
ValueOf () Returns the original value of the Date object. 1 4

share a date formatting method
Share a date formatting method here, using a method similar to DateTime's ToString method in C #:
Copy Code code as follows:

Date.prototype.tostring=function (format) {
var time={};
Time. Year=this.getfullyear ();
Time. Tyear= ("" +time). Year). substr (2);
Time. Month=this.getmonth () +1;
Time. Tmonth=time. Month<10? " 0 "+time. Month:time. Month;
Time. Day=this.getdate ();
Time. Tday=time. Day<10? " 0 "+time. Day:time. Day;
Time. Hour=this.gethours ();
Time. Thour=time. Hour<10? " 0 "+time. Hour:time. Hour;
Time.hour=time. Hour<13?time. Hour:time. Hour-12;
Time. Thour=time.hour<10? " 0 "+time.hour:time.hour;
Time. Minute=this.getminutes ();
Time. Tminute=time. Minute<10? " 0 "+time. Minute:time. Minute;
Time. Second=this.getseconds ();
Time. Tsecond=time. Second<10? " 0 "+time. Second:time. Second;
Time.millisecond=this.getmilliseconds ();

var onumber=time.millisecond/1000;

if (format!=undefined && format.replace (/\s/g, ""). Length>0) {
Format=format
. replace (/yyyy/ig,time. Year)
. replace (/yyy/ig,time. Year)
. replace (/yy/ig,time. Tyear)
. replace (/y/ig,time. Tyear)
. 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);
}
else{
Format=time. year+ "-" +time. month+ "-" +time. Day+ "" +time. hour+ ":" +time. Minute+ ":" +time. Second;
}
return format;
}

var d=new Date ();
Console.log (D.tostring ()); 2011-12-29 11:29:43
Console.log (D.tostring ("")); 2011-12-29 11:29:43
Console.log (d.tostring ("Yyyy-mm-dd")); 2011-12-29
Console.log (d.tostring ("HH:mm:ss")); 11:29:43
Console.log (d.tostring ("Yyyy-mm-dd HH:mm:ss")); 2011-12-29 11:29:43
Console.log (d.tostring ("yyyy mm month DD Day HH:MM:SS")); December 29, 2011 11:29:43
Console.log (d.tostring ("Yyyy-mm-dd HH:mm:ss FFF")); 2011-12-29 11:29:43 862
Related Article

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.