JavaScript Time Format Conversion Summary
1. Current system locale format (tolocaledatestring and tolocaletimestring)
Example: (new Date ()). toLocaleDateString () + "" + (new Date ()). toLocaleTimeString ()
Results: January 29, 2008 16:13:11
2. Ordinary strings (todatestring and toTimeString)
Example: (new Date ()). toDateString () + "" + (new Date ()). toTimeString ()
Results: Tue Jan 16:13:11 utc+0800
3. Greenwich Mean Time (togmtstring)
Example: (New Date ()). toGMTString ()
Results: Tue, Jan 08:13:11 UTC
4. Global Standard Time (toutcstring)
Example: (New Date ()). toUTCString ()
Results: Tue, Jan 08:13:11 UTC
5.Date Object String (toString)
Example: (New Date ()). ToString ()
Results: Tue Jan 16:13:11 utc+0800 2008
var today = new Date ();
document.write (today); In IE: Sun Mar 9 09:42:27 utc+0800 2008; in Firefox: Sun Mar 09:42:27 gmt+0800
var sentence = today.tostring ();
document.write (sentence);//In IE: Sun Mar 9 09:44:42 utc+0800 2008; in Firefox: Sun Mar, 09:44:42 gmt+0800
var GMT = today.togmtstring ();
document.write (GMT);//In IE Sun, 9 mar 01:46:38 UTC; In Firefox: Sun, Mar 01:46:38 GMT
var UTC = Today.toutcstring ();
document.write (UTC);//In IE Sun, 9 Mar 01:46:38 UTC; In Firefox: Sun, Mar 01:46:38gmt
var local = today.tolocalestring ();
document.write (local);//in IE in March 9, 2008 9:48:26; in Firefox: March 9, 2008 9:48:26
getFullYear: Obtain a 4-digit integer;
GetMonth: Get an integer between 0-11;
GetDate: Get an integer between 1-31;
GetDay: Get an integer between 0-6 and 0 for Sunday;
GetHours: Get an integer between 0-23;
getminutes: Get an integer between 0-59;
Getseconds: Get an integer between 0-59;
GetTime: Gets the number of milliseconds starting from the UTC era;
getTimezoneOffset: Gets the difference between local time and GMT time, expressed in minutes; typically:-480 (8 hours apart)
*******************************************************************************
Js/formatdate.js//implement GMT conversion to. Current system locale format
function Dateutil () {}
/**
* Function: Format time
* Example: Dateutil.format ("Yyyy/mm/dd", "Thu Nov 9 20:30:37 utc+0800 2006");
* Back: 2006/11/09
*/
Dateutil.format=function (fmtcode,date) {
var result,d,arr_d;
var patrn_now_1=/^y{4}-m{2}-d{2}\sh{2}:m{2}:s{2}$/;
var patrn_now_11=/^y{4}-m{1,2}-d{1,2}\sh{1,2}:m{1,2}:s{1,2}$/;
var patrn_now_2=/^y{4}\/m{2}\/d{2}\sh{2}:m{2}:s{2}$/;
var patrn_now_22=/^y{4}\/m{1,2}\/d{1,2}\sh{1,2}:m{1,2}:s{1,2}$/;
var patrn_now_3=/^y{4} Year m{2} month d{2} Day \sh{2} m{2} minutes s{2} seconds $/;
var patrn_now_33=/^y{4} Year m{1,2} month d{1,2} Day \sh{1,2} m{1,2} minutes s{1,2} seconds $/;
var patrn_date_1=/^y{4}-m{2}-d{2}$/;
var patrn_date_11=/^y{4}-m{1,2}-d{1,2}$/;
var patrn_date_2=/^y{4}\/m{2}\/d{2}$/;
var patrn_date_22=/^y{4}\/m{1,2}\/d{1,2}$/;
var patrn_date_3=/^y{4} Year m{2} month d{2} day $/;
var patrn_date_33=/^y{4} Year m{1,2} month d{1,2} day $/;
var patrn_time_1=/^h{2}:m{2}:s{2}$/;
var patrn_time_11=/^h{1,2}:m{1,2}:s{1,2}$/;
var patrn_time_2=/^h{2} m{2} minutes s{2} seconds $/;
var patrn_time_22=/^h{1,2} m{1,2} minutes s{1,2} seconds $/;
if (!fmtcode) {fmtcode= "Yyyy/mm/dd hh:mm:ss";}
if (date) {
D=new date (date);
if (IsNaN (d)) {
MsgBox ("Time parameter illegal \ n Correct time example: \nthu 9 20:30:37 utc+0800 2006\n or \n2006/10/17");
return;}
}else{
D=new Date ();
}
if (Patrn_now_1.test (Fmtcode))
{
Arr_d=splitdate (d,true);
result=arr_d.yyyy+ "-" +arr_d.mm+ "-" +arr_d.dd+ "" +arr_d.hh+ ":" +arr_d.mm+ ":" +ARR_D.SS;
}
else if (Patrn_now_11.test (Fmtcode))
{
Arr_d=splitdate (d);
result=arr_d.yyyy+ "-" +arr_d.mm+ "-" +arr_d.dd+ "" +arr_d.hh+ ":" +arr_d.mm+ ":" +ARR_D.SS;
}
else if (Patrn_now_2.test (Fmtcode))
{
Arr_d=splitdate (d,true);
result=arr_d.yyyy+ "/" +arr_d.mm+ "/" +arr_d.dd+ "" +arr_d.hh+ ":" +arr_d.mm+ ":" +ARR_D.SS;
}
else if (Patrn_now_22.test (Fmtcode))
{
Arr_d=splitdate (d);
result=arr_d.yyyy+ "/" +arr_d.mm+ "/" +arr_d.dd+ "" +arr_d.hh+ ":" +arr_d.mm+ ":" +ARR_D.SS;
}
else if (Patrn_now_3.test (Fmtcode))
{
Arr_d=splitdate (d,true);
result=arr_d.yyyy+ "Year" +arr_d.mm+ "month" +arr_d.dd+ "Day" + "+arr_d.hh+" when "+arr_d.mm+" points "+arr_d.ss+" seconds ";
}
else if (Patrn_now_33.test (Fmtcode))
{
Arr_d=splitdate (d);
result=arr_d.yyyy+ "Year" +arr_d.mm+ "month" +arr_d.dd+ "Day" + "+arr_d.hh+" when "+arr_d.mm+" points "+arr_d.ss+" seconds ";
}
else if (Patrn_date_1.test (Fmtcode))
{
Arr_d=splitdate (d,true);
result=arr_d.yyyy+ "-" +arr_d.mm+ "-" +ARR_D.DD;
}
else if (Patrn_date_11.test (Fmtcode))
{
Arr_d=splitdate (d);
result=arr_d.yyyy+ "-" +arr_d.mm+ "-" +ARR_D.DD;
}
else if (Patrn_date_2.test (Fmtcode))
{
Arr_d=splitdate (d,true);
result=arr_d.yyyy+ "/" +arr_d.mm+ "/" +ARR_D.DD;
}
else if (Patrn_date_22.test (Fmtcode))
{
Arr_d=splitdate (d);
result=arr_d.yyyy+ "/" +arr_d.mm+ "/" +ARR_D.DD;
}
else if (Patrn_date_3.test (Fmtcode))
{
Arr_d=splitdate (d,true);
result=arr_d.yyyy+ "Year" +arr_d.mm+ "month" +arr_d.dd+ "Day";
}
else if (Patrn_date_33.test (Fmtcode))
{
Arr_d=splitdate (d);
result=arr_d.yyyy+ "Year" +arr_d.mm+ "month" +arr_d.dd+ "Day";
}
else if (Patrn_time_1.test (Fmtcode)) {
Arr_d=splitdate (d,true);
result=arr_d.hh+ ":" +arr_d.mm+ ":" +ARR_D.SS;
}
else if (Patrn_time_11.test (Fmtcode)) {
Arr_d=splitdate (d);
result=arr_d.hh+ ":" +arr_d.mm+ ":" +ARR_D.SS;
}
else if (Patrn_time_2.test (Fmtcode)) {
Arr_d=splitdate (d,true);
result=arr_d.hh+ "+arr_d.mm+" minute "+arr_d.ss+" seconds ";
}
else if (Patrn_time_22.test (Fmtcode)) {
Arr_d=splitdate (d);
result=arr_d.hh+ "+arr_d.mm+" minute "+arr_d.ss+" seconds ";
}
else{
MsgBox ("No matching time format!");
Return
}
return result;
};
function Splitdate (D,iszero) {
var Yyyy,mm,dd,hh,mm,ss;
if (Iszero) {
Yyyy=d.getyear ();
Mm= (D.getmonth () +1) <10? " 0 "+ (D.getmonth () +1):d. GetMonth () +1;
Dd=d.getdate () <10? " 0 "+d.getdate ():d. GetDate ();
Hh=d.gethours () <10? " 0 "+d.gethours ():d. getHours ();
Mm=d.getminutes () <10? " 0 "+d.getminutes ():d. getminutes ();
Ss=d.getseconds () <10? " 0 "+d.getseconds ():d. getseconds ();
}else{
Yyyy=d.getyear ();
Mm=d.getmonth (+1);
Dd=d.getdate ();
Hh=d.gethours ();
Mm=d.getminutes ();
Ss=d.getseconds ();
}
return {"yyyy": yyyy, "mm": mm, "DD":d D, "hh": hh, "mm": MM, "ss": ss};
}
function MsgBox (msg) {
Window.alert (msg);
}
Call Method:
Dateutil.format ("Yyyy/mm/dd", "Thu Nov 9 20:30:37 utc+0800 2006")
Dateutil.format ("Yyyy-mm-dd hh:mm:ss", "Thu Nov 9 20:30:37 utc+0800 2006")
JS Time format GMT format conversion