It seems that the Date object of js is not very convenient to use. According to the DateTime of C #,
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. |
The Code is as follows:
// Indicates the moment in 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];