JavaScript Date time and timestamp conversion function sharing _javascript tips

Source: Internet
Author: User

If you simply convert the current time to a timestamp, you can use the new date (). GetTime ()/1000, but if you turn a specific time or date into a Unix timestamp, IE does not support a method with parameters like new date ("2013-1-1") and returns Nan.

For this, I wrote the following functions, support ie6+, Google, Firefox and other browsers:

Copy Code code 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 ("2013-02-03 10:10:10"));
Alert (GetTime ("2013-02-03"));
Alert (GetTime ("2013-02"));
Alert (GetTime ("2013"));

The following function, which converts a timestamp into a date format, supports a custom date format that is similar to the PHP date function, and also supports browsers such as ie6+, Google, and Firefox. This function is realized by netizens, I have time to write a ^_^

Copy Code code 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", "St.", "nd", "rd", "St"};
var txt_months = ["", "January", "February", "March", "April", "may", "June", "July", "August", "September", "October", "N Ovember "," 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 ()-A;
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 (Jsdate.getfullyear ()-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 + 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 () *) + 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 = "a" +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 ', ' 1355252653 '));

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.