Date operation datetime Function Code implemented by JS

Source: Internet
Author: User

Method annotation:

Add the specified number of days to the value of this instance.

Add the specified number of hours to the value of this instance.

Add the specified number of minutes to the value of this instance.

Add the specified number of milliseconds to the value of this instance.

Add the specified number of months to the value of this instance.

Add the specified number of seconds to the value of this instance.

Add the specified number of copies per year to the value of this instance.

Compare the Instance value with the specified date value and indicate whether the instance is earlier than, equal to, or later than the specified date value.

Returns a new datetime object with the same value.

Returns a value indicating whether the instance is equal to the specified datetime instance.

Obtain the date part of the instance.

Obtain the date represented by this instance as the day of the month.

Obtain the date of the week for this instance.

Obtains the hour of the specified date.

Obtains the minute of the date represented by this instance.

Obtains the millisecond portion of the date represented by this instance.

Obtains the month of the date represented by this instance.

Obtain the datetime object of this instance from January 1, 1st day of next month.

Obtains the datetime object of this instance for the next Sunday.

Obtains the datetime object of this instance for the next Sunday.

Obtains the second part of the date represented by this instance.

Returns the date value of this instance.

Obtains the year of the date that this instance represents.

Indicates whether the instance is a datetime object.

Converts the value of the current datetime object to its equivalent short date string representation.

Converts the value of the current datetime object to its equivalent short-time string representation.

Converts the value of the current datetime object to its equivalent string representation.

Verify that the method parameters of the add series are valid

Method inherited from date

Compare two instances of datetime and return their relative values.

Returns the number of days in the specified year and month.

Returns a value indicating whether the two instances of datetime are equal.

Returns the indication of whether the specified year is a leap year.

Get a datetime object, which is set to the current date and time on this computer, expressed as local time.

Converts the specified string representation of date and time to its equivalent datetime.

Obtain the current date. The time composition is set to 00:00:00.

CopyCode The Code is as follows: // indicates the moment of the time, usually expressed by the date and the time of the day.
Function datetime (year, month, day, hour, Min, SEC, millisec ){
VaR d = new date ();

If (Year | year = 0 ){
D. setfullyear (year );
}
If (month | month = 0 ){
D. setmonth (month-1 );
}
If (Day | day = 0 ){
D. setdate (day );
}
If (hour | hour = 0 ){
D. sethours (hour );
}
If (min | min = 0 ){
D. setminutes (min );
}
If (SEC | sec = 0 ){
D. setseconds (SEC );
}
If (millisec | millisec = 0 ){
D. setmilliseconds (millisec );
}
// Add the specified number of days to the value of this instance.
This. adddays = function (value ){
If (! Validateaddmethodparam (value )){
Return NULL;
}
VaR result = This. Clone ();
Result. getvalue (). setdate (result. getday () + value );
Return result;
}
// Add the specified hours to the Instance value.
This. addhours = function (value ){
If (! Validateaddmethodparam (value )){
Return NULL;
}
VaR result = This. Clone ();
Result. getvalue (). sethours (result. gethour () + value );
Return result;
}
// Add the specified number of minutes to the value of this instance.
This. addminutes = function (value ){
If (! Validateaddmethodparam (value )){
Return NULL;
}
VaR result = This. Clone ();
Result. getvalue (). setminutes (result. getminute () + value );
Return result;
}
// Add the specified number of milliseconds to the value of this instance.
This. addmilliseconds = function (value ){
If (! Validateaddmethodparam (value )){
Return NULL;
}
VaR result = This. Clone ();
Result. getvalue (). setmilliseconds (result. getmillisecond () + value );
Return result;
}
// Add the specified number of months to the value of this instance.
This. addmonths = function (value ){
If (! Validateaddmethodparam (value )){
Return NULL;
}
VaR result = This. Clone ();
Result. getvalue (). setmonth (result. getvalue (). getmonth () + value );
Return result;
}
// Add the specified number of seconds to the value of this instance.
This. addseconds = function (value ){
If (! Validateaddmethodparam (value )){
Return NULL;
}
VaR result = This. Clone ();
Result. getvalue (). setseconds (result. getsecond () + value );
Return result;
}
// Add the specified number of copies per year to the value of this instance.
This. addyears = function (value ){
If (! Validateaddmethodparam (value )){
Return NULL;
}
VaR result = This. Clone ();
Result. getvalue (). setfullyear (result. getyear () + value );
Return result;
}
// Compare the Instance value with the specified date value and indicate whether the instance is earlier than, equal to, or later than the specified date value.
This. compareto = function (other ){
VaR internalticks = Other. gettime ();
VaR num2 = D. gettime ();
If (num2> internalticks)
{
Return 1;
}
If (num2 <internalticks)
{
Return-1;
}
Return 0;
}
// Return a new datetime object with the same value
This. Clone = function (){
Return new datetime (
This. getyear ()
, This. getmonth ()
, This. getday ()
, This. gethour ()
, This. getminute ()
, This. getsecond ()
, This. getmillisecond ());
}
// Return a value indicating whether the instance is equal to the specified datetime instance.
This. Equals = function (other ){
Return this. compareto (other) = 0;
}
// Obtain the date part of the instance.
This. getdate = function (){
VaR result = new datetime (D. getfullyear (), D. getmonth (), D. getdate (), 0, 0, 0, 0 );
Return result;
}
// Obtain the date represented by this instance as the day of the month.
This. getday = function (){
Return D. getdate ();
}
// Obtain the day of the week for this instance.
This. getdayofweek = function (){
Return D. getday ();
}
// Obtain the hour of the specified date.
This. gethour = function (){
Return D. gethours ();
}
// Obtain the minute of the date represented by this instance.
This. getminute = function (){
Return D. getminutes ();
}
// Obtain the millisecond portion of the date represented by this instance.
This. getmillisecond = function (){
Return D. getmilliseconds ();
}
// Obtain the month of the date represented by this instance.
This. getmonth = function (){
Return D. getmonth () + 1;
}
// Obtain the datetime object of this instance in December 1st day of next month
This. getnextmonthfirstday = function (){
VaR result = new datetime (this. getyear (), this. getmonth (), 1, 0, 0, 0, 0 );
Result = result. addmonths (1 );
Return result;
}
// Obtain the datetime object of this instance for the next Sunday
This. getnextweekfirstday = function (){
VaR result = This. getdate ();
Return result. adddays (7-result. getdayofweek ());
}
// Obtain the datetime object of this instance for the next Sunday
This. getnextyearfirstday = function (){
Return new datetime (this. getyear () + 1, 1, 1, 0, 0, 0, 0 );
}
// Obtain the second part of the date represented by this instance.
This. getsecond = function (){
Return D. getseconds ();
}
// Returns the date value of this instance.
This. getvalue = function (){
Return D;
}
// Obtain the year of the date represented by this instance.
This. getyear = function (){
Return D. getfullyear ();
}
// Indicates whether the instance is a datetime object.
This. isdatetime = function (){}
// Convert the value of the current datetime object to its equivalent short date string representation.
This. toshortdatestring = function (){
VaR result = "";
Result = D. getfullyear () + "-" + (D. getmonth () + 1) + "-" + D. getdate ();
Return result;
}
// Convert the value of the current datetime object to its equivalent short-time string representation.
This. toshorttimestring = function (){
VaR result = "";
Result = D. gethours () + ":" + D. getminutes () + ":" + D. getseconds ();
Return result;
}
// Convert the value of the current datetime object to its equivalent string representation.
This. tostring = function (Format ){
If (typeof (Format) = "string "){

}
Return this. toshortdatestring () + "" + this. toshorttimestring ();
}
// Verify that the method parameters of the add series are valid
Function validateaddmethodparam (PARAM ){
If (typeof (PARAM )! = "Number "){
Return false;
}
Return true;
}
// Inherit the method from date
This. gettime = function (){
Return D. gettime ();
}
}

// Compare the two instances of datetime and return their relative values.
Datetime. Compare = function (D1, D2 ){
Return d1.compareto (D2 );
}
// Return the number of days in the specified year and month.
Datetime. daysinmonth = function (year, month ){
If (month <1) | (month> 12 ))
{
Return "month [" + month + "] Out of range ";
}
VaR numarray = datetime. isleapyear (year )? Datetime. daystomonth366: datetime. daystomonth365;
Return (numarray [month]-numarray [month-1]);
}
// Return a value indicating whether the two instances of datetime are equal.
Datetime. Equals = function (D1, D2 ){
Return d1.compareto (D2) = 0;
}
// Returns the indication of whether the specified year is a leap year.
Datetime. isleapyear = function (year)
{
If (year <1) | (Year> 0x270f ))
{
Return "year [" + year + "] Out of range ";
}
If (Year % 4 )! = 0)
{
Return false;
}
If (Year % 100) = 0)
{
Return (Year % 400) = 0 );
}
Return true;
}
// Obtain a datetime object. The object is set to the current date and time on this computer, which indicates the local time.
Datetime. Now = new datetime ();
// Convert the specified string representation of the date and time to its equivalent datetime.
Datetime. parse = function (s ){
VaR result = new datetime ();
VaR value = result. getvalue ();
Value. sethours (0, 0, 0 );
VaR daterex =/\ B [1-2] [0-9] [0-9] [0-9] [-] \ D {1, 2} [-] \ D {1, 2} \ B/I;
If (daterex. test (s )){
VaR datestr = S. Match (daterex) [0];
Try {
VaR dateparts = datestr. Split ("-");
VaR year = dateparts [0]-0;
VaR month = dateparts [1]-1;
VaR day = dateparts [2]-0;
Value. setfullyear (year, month, day );
} Catch (Ex ){
Return NULL;
}
VaR timerex =/\ B \ D {1, 2} [:] \ D {1, 2} [:] \ D {1, 2} \ B/I;
If (timerex. test (s )){
VaR timestr = S. Match (timerex) [0];
Try {
VaR timeparts = timestr. Split (":");
VaR hour = timeparts [0]-0;
VaR min = timeparts [1]-0;
VaR sec = timeparts [2]-0;
Value. sethours (hour, Min, Sec );
} Catch (Ex ){

}
}
} Else {
Return NULL;
}
Return result;
}
// Obtain the current date. The time composition is set to 00:00:00.
Datetime. Today = new datetime (null, 0, 0, 0 );

// Static field
Datetime. daystomonth365 = [0, 0x1f, 0x3b, 90,120, 0x97, 0xb5, 0xd4, 0xf3, 0x111, 0x130, 0x14e, 0x16d];
Datetime. daystomonth366 = [0, 0x1f, 60, 0x5b, 0x79, 0x98, 0xb6, 0xd5, 0xf4, 0x112, 0x131, 0x14f, 0x16e];

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.