JS Time Processing

Source: Internet
Author: User

1. Format time

function Getdatetimeformatter (value) {
if (value = = undefined) {
Return "";
}
/*json format time to JS time format */
Value = Value.substr (1, value.length-2);
var obj = eval (' (' + ' {date:new ' + value + '} ' + ') ');
var dateValue = obj["Date"];
if (Datevalue.getfullyear () < 1900) {
Return "";
}

Return Datevalue.format ("Yyyy-mm-dd HH:MM:ss");
}

2. Get the current time

function Getcurtime () {
var d = new Date ();
var years = D.getfullyear ();
var month = Add_zero (D.getmonth () + 1);
var days = Add_zero (D.getdate ());
var hours = Add_zero (D.gethours ());
var minutes = Add_zero (D.getminutes ());
var seconds = Add_zero (D.getseconds ());
var ndate = years + "-" + month + "-" + Days + "-" + Hours + ":" + minutes + ":" + seconds;
return ndate;
}
function Add_zero (temp) {
if (temp <) return "0" + temp;
else return temp;
}

3. Time Comparison

eg. var a = Getdatetimeformatter (T1);

var b =getcurtime ();

if (a>b) {}//so you can


4.ie Time compatibility issues

JS time recommended format: 2000/01/01

If you use 2000-01-01 in IE8 and the following versions, there are compatibility issues

var dateValue ="2013-01-01 01:01:00";var Createdt =New Date ();// creation time Createdt.setfullyear (Datevalue.substr (0, 42)-1); Createdt.setdate (Datevalue.substr (2)); Createdt.sethours (Datevalue.substr (2)); Createdt.setminutes (Datevalue.substr (2)); Createdt.setseconds (0);          

/** * Parse the input datestr and return the date type. * DATESTR:XXXX-XX-XX*/var Strarray = datestr.split (- "); if (strarray.length = = 3 return new Date ( Strarray[0 ", Strarray[2else{return new Date ();}           

5.Date method Extension
/*Function: Date plus n days parameter: N is the number of days returned: N Days after date*/Date.prototype.addDays = Date.prototype.addDays | |function (n) {This.setdate (This.getdate () +n);ReturnThis;}/*function: Date minus n days parameter: N is the number of days returned: N Days after date*/Date.prototype.minusDays = Date.prototype.minusDays | |function (n) {This.setdate (This.getdate ()-n);ReturnThis;}/*Function: Date plus n days parameter: N is the number of days returned: N Days after date*/Date.prototype.getWeek = Date.prototype.getWeek | |function (date) {//var n = date.getday ();var n =This. GetDay ();var rtnstr ="";Switch(n) {Case0: {rtnstr ="Sunday";Break; }Case1: {rtnstr ="Monday";Break; }Case2: {rtnstr ="Tuesday";Break; }Case3: {rtnstr ="Wednesday";Break; }Case4: {rtnstr ="Thursday";Break; }Case5: {rtnstr ="Friday";Break; }Case6: {rtnstr ="Saturday";Break; }Default: {Rtnstr ="";Break; } }ReturnRtnstr;}/*Function: Converts a string to a Date object parameter: A string returned as a yyyy-mm-dd or YYYY/MM/DD: Date Object Note: Direct instantiation of date objects, such as new date ("2011-04-06") is not supported under IE*/Date.prototype.convertDate =function (date) {var flag =True;var Datearray = Date.split ("-");if (datearray.length! =3) {Datearray = Date.split ("/");if (datearray.length! =3) {ReturnNull; }//Flag = false;}var newdate =NewDate ();If(flag) {//Month starts from 0 newdate.setfullyear (datearray[0], datearray[1]-1, datearray[2]); }Else{Newdate.setfullyear (datearray[2], datearray[1]-1, datearray[0]); } newdate.sethours (0,0,0);ReturnNewdate;};/*Function: Calculates the difference parameter between two dates: date is a DateTime object flag:ms-milliseconds, S-second, M-Min, H-hour, D-Day, M-month, Y-year return: current date and date two milliseconds/second/minute/hour/day*/Date.prototype.dateDiff =function (date, flag) {var mscount =24 *60 *60 *1000;This.sethours (0,0,0,0); Date.sethours (0,0,0,0);var diff =This.gettime ()-Date.gettime ();return Math.floor (diff/Mscount);Switch(flag) {Case"Ms": Mscount =1;Break;Case"S": Mscount =1000;Break;Case"M": Mscount = 60 * 1000; Break Case "h": Mscount = 60 * 60 * 1000; Break Case "D": Mscount = 24 * 60 * 60 * 1000; Break } return Math.floor (Diff/mscount);};/ * Function: Format date parameter: formatstr-format string D: Displays the day as a number without leading zeros, such as 1DD: Displays the day as a number with leading zeros, such as 01ddd: Displays the day as an abbreviated form, such as SUNDDDD: Displays the day as the full name, such as Sundaym: Displays the month as a number without leading zeros, as shown in January as 1MM: Displays the month as a number with leading zeros, such as 01MMM: Displays the month as an abbreviated form, such as janmmmm: Displays the month as the full month name, such as januaryyy: Displays the year in two-digit format yyyy: Year in four-digit format h: Displays the hour as a number without leading zeros using a 12-hour system Note | | Usage hh: Displays the hour as a number h with leading zeros using a 12-hour system: Use a 24-hour system to display the hour as a number without leading zeros hh: Displays the hour as a number m with leading zeros using a 24-hour system: Displays the minute as a number with no leading zeros mm: Displays the minute as a number s with leading zeros: Displays the second as a number SS without leading zeros : Displays seconds as a leading zero number L: Displays milliseconds as a number without leading zeros ll: Displays milliseconds as a number with leading zeros TT: Display AM/PMTT: Show AM/PM return: formatted date */date.prototype.format = function ( FORMATSTR) {var date = this;/* Function: Fills the 0-character parameter: value-the string to fill, length-total length returns: The populated string */var zeroize = function (value, lengt h) {if (!length) {length = 2;} value = new String (value); for (var i = 0, zeros = "; < (length-value.length); i++) {zeros + = ' 0 ';} return zeros + value; }; return Formatstr.replace (/"[^"]* "|" [^ ']* ' |\b (?:d {1,4}| M{1,4}|yy (?: YY)? | ([HHMSTT]) \1?| [LLZ]) \b/g, function ($) {switch ($) {case ' d ': return Date.getdate (); case ' DD ': Return Zeroize (Date.getdate ()); case ' DDD ' : return [' Sun ', ' Mon ', ' Tue ', ' Wed ', ' Thr ', ' Fri ', ' Sat '][date.getday ()]; Case ' dddd ': return [' Sunday ', ' Monday ', ' Tuesday ', ' Wednesday ', ' Thursday ', ' Friday ', ' Saturday '][date.getday ()]; Case ' M ': Return Date.getmonth () + 1; Case ' MM ': Return Zeroize (Date.getmonth () + 1); Case ' MMM ': return [' Jan ', ' Feb ', ' Mar ', ' April ', ' may ', ' June ', ' Jul ', '][date.getmonth ', ' Sep ', ' Oct ', ' Nov ', ' Dec ' ()]; Case ' MMMM ': return [' January ', ' February ', ' March ', ' April ', ' may ', ' June ', ' July ', ' August ', ' September ', ' October ', ' No ' Vember ', ' December '][date.getmonth ()]; Case ' yy ': return new String (Date.getfullyear ()). substr (2); Case ' yyyy ': return Date.getfullyear (); Case ' H ': return date.gethours ()% 12 | | 12; Case ' hh ': Return Zeroize (date.gethours ()% 12 | | 12); Case ' H ': return date.gethours (); Case ' HH ': Return Zeroize (Date.gethours ()); Case ' m ': returnDate.getminutes (); Case ' mm ': Return Zeroize (Date.getminutes ()); Case ' s ': return Date.getseconds (); Case ' SS ': Return Zeroize (Date.getseconds ()); Case ' l ': return Date.getmilliseconds (); Case ' ll ': Return Zeroize (Date.getmilliseconds ()); Case ' TT ': Return date.gethours () < 12? ' Am ': ' PM '; Case ' TT ': Return date.gethours () < 12? ' AM ': ' PM '; } });}

6. Time Stamp
Gets the current timestamp: The first method:var timestamp = date.parse (new Date ()); Result: 1280977330000 the second method:var timestamp = (  New date ()). ValueOf (); results: 1280977330748 the third method:var timestamp= (new date ()). GetTime () ; Results: 1280977330748 First: The timestamp was obtained by changing the millisecond to 000, and the second and third was to obtain the timestamp of the current millisecond. 

Sometimes the return time format is encountered as a JSON string, which turns into a timestamp:

parseint ((DATEJSONSTR). Replace ("/date (", ""). Replace (")/", ""), 10);

JS Time Processing

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.