Share the conversion functions of JavaScript datetime and timestamp, javascript

Source: Internet
Author: User

Share the conversion functions of JavaScript datetime and timestamp, javascript

If you only convert the current time to a timestamp, you can directly use new Date (). getTime ()/1000; however, if a specific time or Date is converted to a Unix timestamp, ie does not support parameters such as new Date ("2013-1-1, naN is returned.

In this regard, I wrote the following functions that support browsers such as ie6 +, Google, and Firefox:

Copy codeThe Code is as follows:
Function getTime (day ){
Re =/(\ d {4 })(? :-(\ D {1, 2 })(? :-(\ D {1, 2 }))?)? (? : \ S + (\ d {1, 2}) :( \ d {1, 2}) :( \ d {1, 2 }))? /. Exec (day );
Return new Date (re [1], (re [2] | 1)-1, re [3] | 1, re [4] | 0, re [5] | 0, re [6] | 0 ). getTime ()/1000;
}

// Test
Alert (getTime ("10:10:10 "));
Alert (getTime ("2013-02-03 "));
Alert (getTime ("2013-02 "));
Alert (getTime ("2013 "));

The following function converts a timestamp into a date format and supports custom date formats. The effect is similar to the date function of PHP. It also supports browsers such as ie6 +, Google, and Firefox. This function is implemented by netizens. I will write it out later. ^_^

Copy codeThe Code is as follows:
Function date (format, timestamp ){
Var a, jsdate = (timestamp )? New Date (timestamp * 1000): new Date ());
Var pad = function (n, c ){
If (n = n + ""). length <c ){
Return new Array (++ c-n. length). join ("0") + n;
} Else {
Return n;
}
};
Var txt_weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
Var txt_ordin = {1: "st", 2: "nd", 3: "rd", 21: "st", 22: "nd", 23: "rd ", 31: "st "};
Var txt_months = ["", "January", "February", "March", "April", "May", "June", "July", "August ", "September", "October", "November", "December"];
Var f = {
// Day
D: function () {return pad (f. j (), 2 )},
D: function () {return f. l (). substr (0, 3 )},
J: function () {return jsdate. getDate ()},
L: function () {return txt_weekdays [f. w ()]},
N: function () {return f. w () + 1 },
S: function () {return txt_ordin [f. j ()]? Txt_ordin [f. j ()]: 'th '},
W: function () {return jsdate. getDay ()},
Z: function () {return (jsdate-new Date (jsdate. getFullYear () + "/1/1")/864e5> 0 },

// Week
W: function (){
Var a = f. z (), B = 364 + f. L ()-;
Var nd2, nd = (new Date (jsdate. getFullYear () + "/1/1"). getDay () | 7)-1;
If (B <= 2 & (jsdate. getDay () | 7)-1) <= 2-B ){
Return 1;
} Else {
If (a <= 2 & nd> = 4 & a> = (6-nd )){
Nd2 = new Date (FIG ()-1 + "/12/31 ");
Return date ("W", Math. round (nd2.getTime ()/1000 ));
} Else {
Return (1 + (nd <= 3? (A + nd)/7): (a-(7-nd)/7)> 0 );
}
}
},

// Month
F: function () {return txt_months [f. n ()]},
M: function () {return pad (f. n (), 2 )},
M: function () {return f. F (). substr (0, 3 )},
N: function () {return jsdate. getMonth () + 1 },
T: function (){
Var n;
If (n = jsdate. getMonth () + 1) = 2 ){
Return 28 + f. L ();
} Else {
If (n & 1 & n <8 |! (N & 1) & n> 7 ){
Return 31;
} Else {
Return 30;
}
}
},

// Year
L: function () {var y = f. Y (); return (! (Y & 3) & (y % 1e2 |! (Y % 4e2 )))? 1: 0 },
// O not supported yet
Y: function () {return jsdate. getFullYear ()},
Y: function () {return (jsdate. getFullYear () + ""). slice (2 )},

// Time
A: function () {return jsdate. getHours ()> 11? "Pm": "am "},
A: function () {return f. a (). toUpperCase ()},
B: function (){
// Peter paul koch:
Var off = (jsdate. getTimezoneOffset () + 60) * 60;
Var theSeconds = (jsdate. getHours () * 3600) + (jsdate. getMinutes () * 60) + jsdate. getSeconds () + off;
Var beat = Math. floor (theSeconds/86.4 );
If (beat> 1000) beat-= 1000;
If (beat <0) beat + = 1000;
If (String (beat). length = 1) beat = "00" + beat;
If (String (beat). length = 2) beat = "0" + beat;
Return beat;
},
G: function () {return jsdate. getHours () % 12 | 12 },
G: function () {return jsdate. getHours ()},
H: function () {return pad (f. g (), 2 )},
H: function () {return pad (jsdate. getHours (), 2 )},
I: function () {return pad (jsdate. getMinutes (), 2 )},
S: function () {return pad (jsdate. getSeconds (), 2 )},
// U not supported yet

// Timezone
// E not supported yet
// I not supported yet
O: function (){
Var t = pad (Math. abs (jsdate. getTimezoneOffset ()/60*100), 4 );
If (jsdate. getTimezoneOffset ()> 0) t = "-" + t; else t = "+" + t;
Return t;
},
P: function () {var O = f. O (); return (O. substr (0, 3) + ":" + O. substr (3, 2 ))},
// T not supported yet
// Z not supported yet

// Full Date/Time
C: function () {return f. Y () + "-" + f. m () + "-" + f. d () + "T" + f. h () + ":" + f. I () + ":" + f. s () + f. P ()},
// R not supported yet
U: function () {return Math. round (jsdate. getTime ()/1000 )}
};

Return format. replace (/[\]? ([A-zA-Z])/g, function (t, s ){
If (t! = S ){
// Escaped
Ret = s;
} Else if (f [s]) {
// A date function exists
Ret = f [s] ();
} Else {
// Nothing special
Ret = s;
}
Return ret;
});
}

// Test
Alert (date ('Y-m-d H: I: s', (new Date). getTime ()/1000 ));
Alert (date ('Y-m-d ', (new Date). getTime ()/1000 ));
Alert (date ('Y-m-d H: I: s', '123 '));

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.