Date-time processing functions in JavaScript

Source: Internet
Author: User
Tags diff getdate month name
The code is as follows Copy Code
/**
* Convert string to date Object
* Parameter: Date string For example: 2011-04-19 or 19/04/2011
* Return: Date Object dates
* Note: IE does not support direct instantiation of date objects, such as new date ("2011-04-06")
**/
Date.prototype.converdate=function (date) {
var flag=true;
var datearray=date.splite ("-");
if (datearray.length!=3) {
Datearray=date.splite ("/");
if (dataarray.length!=3) {
return null;
}
Flag=false;
}
var newdate=new Date ();
if (flag) {
Month starting 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);
return newdate;
};
/*
Demo
<script type= "Text/javascript" >
Document.writeln (New Date (). Convertdate ("7/12/2011"));
</script>
*/




/*
Function: To determine whether a year is a leap years
Return: is a leap year
*/

Date.prototype.isLeapYear = function () {
var year = This.getfullyear ();
Return (year% 4 = 0) && (year%!= 0 | |-year% 400 = 0);
}
/*
<script type= "Text/javascript" >
Document.writeln (New Date (). Convertdate ("2000-04-08"). Isleapyear () + "<BR>");
Document.writeln (New Date (). Convertdate ("2011-04-08"). Isleapyear () + "<BR>");
</script>
Results:
True
False
*/

/*
Functions: Calculates the difference between two dates
Parameter: Date is a Day object
flag:ms-millisecond, S-second, M-minute, H-hour, D-Day, M-month, y-year
Returns: Millisecond/sec/min/hour/day for the difference between the current date and date two dates
*/
Date.prototype.dateDiff = function (date, flag) {
var Mscount;
var diff = this.gettime ()-date.gettime ();
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);
};
/*
<script type= "Text/javascript" >
var D1 = new Date (). Convertdate ("2011-04-08");
var d2 = new Date (). Convertdate ("2011-04-07");
Document.writeln (D1.datediff (D2, ' d '));
</script>
*/

/*

Functions: Formatting dates
Parameters: formatstr-format string
D: Displays the day as a number with no leading zeros, such as 1
DD: Displays the day as a number with a leading zero, such as 01
DDD: Displays the day as an abbreviated form, such as sun
DDDD: Displays the day as full name, such as Sunday
<span class= "Goog_qs-tidbit goog_qs-tidbit-0" >m: Displays the month as a number with no leading zeros, as shown in January as 1
MM: Displays the month as a </SPAN> number with leading zeros, such as 01
MMM: Displays the month as an abbreviated form, such as a few
MMMM: Displays the month as full month name, such as January
YY: Display year in two-digit number format
YYYY: Display year in four-digit number format
H: Use a 12-hour system to display hours without leading zeros, note | | Use of
H: Use a 24-hour system to display the hours as a number with no leading zeros
HH: Use a 24-hour system to display hours as numbers with leading zeros
M: Displays the minute as a number without a leading zero
MM: Displays the minute as a number with a leading zero
S: Displays seconds as numbers with no leading zeros
SS: Displays seconds as digits with leading zeros
L: Displays the millisecond as a number without a leading zero
LL: Displays the millisecond as a number with a leading zero
TT: Show am/pm
TT: Show am/pm
Back: Formatted date
*/
Date.prototype.format = function (formatstr) {
var date = this;

/*
Functions: Populating 0 characters
Parameters: value-need to fill the string, length-total length
Returns: The filled string
*/
var zeroize = function (value, length) {
if (!length) {
length = 2;
}
Value = new String (value);
for (var i = 0, zeros = '; i < (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 [' Out of the window ', ' Feb ', ' Mar ', ' Apr ', ' may ', ' June ', ' o ', ' Aug ', ' Sep ', ' Oct ', ' Nov ', ' Dec '][date.getmonth ()];
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 ': return Date.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 ';
}
});
}

/**
<script type= "Text/javascript" >
Document.writeln (New Date (). Format ("Yyyy-mm-dd hh:mm:ss"));
</script>
Results:
2011-04-08 10:13:37
*//

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.