Format and time-processing function of ExtJs JS function __

Source: Internet
Author: User
Tags getdate

The following example shows how to interpret date and time patterns in the U.S. language environment. The given date and time is 2001-07-04 12:08:56 for the Pacific Time zone in the United States.

Date and Time pattern results
"YYYY. Mm.dd G ' at ' HH:mm:ss z ' 2001.07.04 AD at 12:08:56 PDT
"EEE, MMM D, ' yy" Wed, June 4, ' 01
"H:mm a" 12:08 PM
"HH ' o ' clock ' A, zzzz" O ' clock PM, Pacific Daylight Time
"K:mm A, z" 0:08 PM, PDT
"Yyyyy. MMMMM.DD GGG hh:mm aaa "02001.july.04 AD 12:08 PM
"EEE, D MMM yyyy HH:mm:ss Z" Wed, 4 June 2001 12:08:56-0700
"Yymmddhhmmssz" 010704120856-0700
"Yyyy-mm-dd ' T ' HH:mm:ss." Sssz "2001-07-04t12:08:56.235-0700

In ExtJS, formatting is not consistent with the above. The following can also be applied to Gwt-ext

Sample data:

' Wed 2007 15:05:01 GMT-0600 (Central Standard Time) '

Format character Output description

------  ----------  --------------------------------------------------------------

d The number of days in October, two digits, insufficient bit to fill "0"

D Wed The current week's abbreviation, three letters

J The number of days in October, do not fill "0"

L Wednesday the full spelling of the current week

S th English suffix for the ordinal number of days of the month, 2 characters (used with the format character "J")

W 31 days in the week (1~7)

Z Days of 91 years (0~365)

Number of weeks in 11 years, two digits (00~52)

F January the full spelling of the current month

M 01 current month, two digits, insufficient bit complement "0"

M the full spelling of the current month, three letters

N 1 The current month, do not fill "0"

T 31 total days of the current month

L 0 whether Leap year ("1" for leap Year, "0" for excepting)

Y 2007 4-digit number of the current year

Y 07 2-digit number of the current year

A PM lowercase "am" and "PM"

A PM "AM" and "PM" uppercase

G 3 12-hour system represents the current number of hours, does not complement the "0"

G 15 24-hour system represents the current number of hours, does not complement the "0"

H 03 12-hour system represents the current number of hours, insufficient bit to fill "0"

H 15 24-hour system represents the current number of hours, insufficient bit to fill "0"

I 05 min Digit complement "0" minutes

s 01 number of seconds with insufficient bit complement "0"

Number of O-0600 in hours and GMT difference

T CST Current system setting time zone

Z-21600 time zone offset in seconds (negative in the West, positive in the east)

Usage Examples: (Note that you must use the convert character "\" before the letter to output it as the letter itself rather than the format character):

var dt = new Date (' 1/10/2007 03:05:01 PM GMT-0600 ');

document.write (Dt.format (' y-m-d ')); 2007-01-10

document.write (Dt.format (' F J, Y, g:i a ')); January, 2007, 3:05 PM

document.write (Dt.format (' L, \\t\\he DS of F Y h:i:s A ')); Wednesday, the 10th of January 2007 03:05:01 PM


var mydate = new Date ();
Mydate.getyear (); Get Current year (2-bit)
Mydate.getfullyear (); Get full year (4-bit, 1970-????)
Mydate.getmonth (); Get Current month (0-11, 0 for January)
Mydate.getdate (); Get the current day (1-31)
Mydate.getday (); Get Current week x (0-6, 0 for Sunday)
Mydate.gettime (); Gets the current time (number of milliseconds since 1970.1.1)
Mydate.gethours (); Get the current number of hours (0-23)
Mydate.getminutes (); Get the current number of minutes (0-59)
Mydate.getseconds (); Get the current number of seconds (0-59)
Mydate.getmilliseconds (); Get current number of milliseconds (0-999)
Mydate.tolocaledatestring (); Get Current date
var mytime=mydate.tolocaletimestring (); Get current time
Mydate.tolocalestring (); Get Date and time

Date Time Script Library method list

Date.prototype.isLeapYear Judge Leap Year
Date.prototype.Format Date format
DATE.PROTOTYPE.DATEADD Date Calculation
Date.prototype.DateDiff Comparison Date Difference
Date.prototype.toString Date-turn string
Date.prototype.toArray date split into arrays
Date.prototype.DatePart some information about the date
Date.prototype.MaxDayOfDate the maximum number of days of the month from which the date is taken
Date.prototype.WeekNumOfYear the first weeks of the year in which the date is judged
Stringtodate String Turn Date type
Isvaliddate Validation Date Validity
Checkdatetime Full Date Time check
Daysbetween Date Days Bad

JS Code:

//---------------------------------------------------
Judging leap years
//---------------------------------------------------
Date.prototype.isLeapYear = function ()
{
Return (0==this.getyear ()%4&& (This.getyear ()%100!=0) | | (This.getyear ()%400==0));
}

//---------------------------------------------------
Date formatting
Format Yyyy/yyyy/yy/yy represents year
Mm/m Month
W/W Week
dd/dd/d/d Date
hh/hh/h/h time
mm/m minutes
SS/SS/S/S seconds
//---------------------------------------------------
Date.prototype.Format = function (FORMATSTR)
{
var str = FORMATSTR;
var Week = [' Day ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six '];

Str=str.replace (/yyyy| Yyyy/,this.getfullyear ());
Str=str.replace (/yy| yy/, (this.getyear ()%) >9? (This.getyear ()%). ToString (): ' 0 ' + (this.getyear ()% 100));

Str=str.replace (/mm/,this.getmonth () >9?this.getmonth (). toString (): ' 0 ' + this.getmonth ());
Str=str.replace (/m/g,this.getmonth ());

Str=str.replace (/w| W/g,week[this.getday ()]);

Str=str.replace (/dd| Dd/,this.getdate () >9?this.getdate (). toString (): ' 0 ' + this.getdate ());
Str=str.replace (/d| D/g,this.getdate ());

Str=str.replace (/hh| Hh/,this.gethours () >9?this.gethours (). toString (): ' 0 ' + this.gethours ());
Str=str.replace (/h| H/g,this.gethours ());
Str=str.replace (/mm/,this.getminutes () >9?this.getminutes (). toString (): ' 0 ' + this.getminutes ());
Str=str.replace (/m/g,this.getminutes ());

Str=str.replace (/ss| Ss/,this.getseconds () >9?this.getseconds (). toString (): ' 0 ' + this.getseconds ());
Str=str.replace (/s| S/g,this.getseconds ());

return str;
}

//+---------------------------------------------------
//| The number of days for two time difference date format is YYYY-MM-DD
//+---------------------------------------------------
function Daysbetween (dateone,datetwo)
{
var onemonth = dateone.substring (5,dateone.lastindexof ('-'));
var oneday = dateone.substring (Dateone.length,dateone.lastindexof ('-') +1);
var oneyear = dateone.substring (0,dateone.indexof ('-'));

var twomonth = datetwo.substring (5,datetwo.lastindexof ('-'));
var twoday = datetwo.substring (Datetwo.length,datetwo.lastindexof ('-') +1);
var twoyear = datetwo.substring (0,datetwo.indexof ('-'));

var cha= (Date.parse (onemonth+ '/' +oneday+ '/' +oneyear ')-date.parse (twomonth+ '/' +twoday+ '/' +twoyear))/86400000);
Return Math.Abs (CHA);
}


//+---------------------------------------------------
//| Date Calculation
//+---------------------------------------------------
DATE.PROTOTYPE.DATEADD = function (strinterval, number) {
var dttmp = this;
Switch (strinterval) {
Case ' s ': return new Date (Date.parse (dttmp) + (1000 * number));
Case ' n ': return new Date (Date.parse (dttmp) + (60000 * number));
Case ' H ': return new Date (Date.parse (dttmp) + (3600000 * number));
Case ' d ': return new Date (Date.parse (dttmp) + (86400000 * number));
Case ' W ': Return new Date (Date.parse (dttmp) + ((86400000 * 7) * number));
Case ' Q ': return new Date (Dttmp.getfullyear (), (Dttmp.getmonth ()) + number*3, Dttmp.getdate (), dttmp.gethours (), Dttmp.getminutes (), dttmp.getseconds ());
Case ' m ': return new Date (Dttmp.getfullyear (), (Dttmp.getmonth ()) + number, dttmp.getdate (), dttmp.gethours (), Dttmp.getminutes (), dttmp.getseconds ());
Case ' Y ': Return new Date ((dttmp.getfullyear () + number), Dttmp.getmonth (), Dttmp.getdate (), dttmp.gethours (), Dttmp.getminutes (), dttmp.getseconds ());
}
}

//+---------------------------------------------------
//| Compare Date Difference dtend format is a date type or a valid date format string
//+---------------------------------------------------
Date.prototype.DateDiff = function (Strinterval, dtend) {&

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.