JS Implementation date Operation class DateTime function code _javascript Skill

Source: Internet
Author: User
Tags getdate local time

Method Annotation:

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

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

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

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

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

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

Adds the specified number of years to the value of this instance.

Compares the value of this instance with the specified date value, and indicates whether this 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 that indicates whether this instance is equal to the specified DateTime instance.

Gets the date part of this instance.

Gets the date represented by this instance as the day ordinal of the month.

Gets the day of the week that this instance represents.

Gets the hour portion of the date represented by this instance.

Gets the minute portion of the date represented by this instance.

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

Gets the month portion of the date represented by this instance.

Gets the DateTime object for the day of the next month of this instance

Gets the next Sunday datetime object for this instance

Gets the next Sunday datetime object for this instance

Gets the second portion of the date represented by this instance.

Returns the date value of this instance

Gets the year portion of the date represented by this instance.

Indicates whether this 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 for the Add series are legitimate

Methods that inherit from date

Compares two instances of DateTime and returns an indication of their relative values.

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

Returns a value that indicates whether two instances of DateTime are equal.

Returns whether the specified year is an indication of leap years.

Gets a DateTime object that is set to the current date and time on this computer, expressed as a local time.

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

Gets the current date with the time component set to 00:00:00.

Copy Code code as follows:



//Represents a moment in time, usually expressed as the date and the time of 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 | | = = 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);


    }


//Adds 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;


    }


//Adds the specified number of hours to the value of this instance.


this. AddHours = function (value) {


if (! Validateaddmethodparam (value)) {


return null;


        }


var result = this. Clone ();


result. GetValue (). Sethours (Result. Gethour () + value);


return result;


    }


//Adds 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;


    }


//Adds 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;


    }


//Adds 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;


    }


//Adds 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;


    }


//Adds the specified number of years 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;


    }    


//Compares the value of this instance with the specified date value and indicates whether this 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;


    }


//Returns a new DateTime object with the same numeric value


this. Clone = function () {


return new DateTime (


This. GetYear ()


, this. GetMonth ()


, this. Getday ()


, this. Gethour ()


, this. Getminute ()


, this. Getsecond ()


, this. Getmillisecond ());


    }


//Returns a value that indicates whether this instance is equal to the specified DateTime instance.


this. Equals = function (other) {


Return This.compareto (Other) = = 0;


    }


//Gets the date part of this instance.


this. GetDate = function () {


var result = new DateTime (D.getfullyear (), D.getmonth (), D.getdate (), 0, 0, 0, 0);


return result;


    }


//Get the date represented by this instance is the day ordinal of the month.


this. Getday = function () {


return d.getdate ();


    }


//Gets the day of the week that this instance represents.


this. GetDayOfWeek = function () {


return D.getday ();


}


//Gets the hour portion of the date represented by this instance.


this. Gethour = function () {


return d.gethours ();


    }


//Gets the minute portion of the date represented by this instance.


this. Getminute = function () {


return d.getminutes ();


    }


//Gets the millisecond portion of the date represented by this instance.


this. Getmillisecond = function () {


return D.getmilliseconds ();


    }


//Gets the month portion of the date represented by this instance.


this. GetMonth = function () {


return D.getmonth () + 1;


    }


//Get the DateTime object for the day of the next month of this instance


this. Getnextmonthfirstday = function () {


var result = new DateTime (this. GetYear (), this. GetMonth (), 1, 0, 0, 0, 0);


result = result. Addmonths (1);


return result;


    }


//Get the next Sunday DateTime object for this instance


this. Getnextweekfirstday = function () {


var result = this. GetDate ();


return result. AddDays (7-result. GetDayOfWeek ());


}


//Get the next Sunday DateTime object for this instance


this. Getnextyearfirstday = function () {


return to New DateTime (this. GetYear () + 1, 1, 1, 0, 0, 0, 0);


    }


//Gets the second portion 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;


    }


//Gets the year portion of the date represented by this instance.


this. getyear = function () {


return D.getfullyear ();


    }


//Indicates whether this instance is a DateTime object


this. Isdatetime = function () {}


//Converts 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;


    }


//Converts 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;


    }


//Converts 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 legal


function Validateaddmethodparam (param) {


if (typeof (param)!= "number") {


return false;


        }


return true;


    }


//Inheriting from date method


this.gettime = function () {


return D.gettime ();


    }


}





//Compares two instances of DateTime and returns an indication of their relative values.


datetime.compare = function (d1, D2) {


return D1.compareto (D2);


}


//Returns 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]);


}


//Returns a value that indicates whether two instances of DateTime are equal.


datetime.equals = function (d1, D2) {


return D1.compareto (d2) = = 0;


}


//Returns whether the specified year is an indication of leap years.


datetime.isleapyear = function (year)


{


if (Year < 1) | | (Year > 0x270f))


{


return "Years [" + year + "] out of range";


}


if (year% 4)!= 0)


{


return false;


}


if ((year% 100) = = 0)


{


return ((year% 400) = = 0);


}


return true;


}


//Gets a DateTime object that is set to the current date and time on this computer, expressed as a local time.


DateTime.Now = new DateTime ();


//Converts the specified string representation of a date and time to its DateTime equivalent.


DateTime.Parse = function (s) {


var result = new DateTime ();


var value = result. GetValue ();


value.sethours (0,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;


}


//Gets the current date, and its time component is set to 00:00:00.


datetime.today = new DateTime (null, NULL, NULL, 0, 0, 0, 0);





//static fields


datetime.daystomonth365 = [0, 0x1f, 0x3b,, 0x97, 0xb5, 0xd4, 0xf3, 0x111, 0x130];


datetime.daystomonth366 = [0, 0x1f, 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.