Small Example of Jquery time verification and Conversion Tool _ jquery-js tutorial

Source: Internet
Author: User
Tags time in milliseconds
This article introduces a small example of Jquery time verification and Conversion Tool. If you need it, refer to it. The Code is as follows:


Var TimeObjectUtil;
/**
* @ Title time tool
* @ Note: If this type is not verified, false is returned.
* @ Author {boonyachengdu@gmail.com}
* @ Date 2013-07-01
* @ Formatter "00:00:00", "2013-07-01"
*/
TimeObjectUtil = {
/**
* Get the current time in milliseconds
*/
GetCurrentMsTime: function (){
Var myDate = new Date ();
Return myDate. getTime ();
},
/**
* Millisecond to time format
*/
LongMsTimeConvertToDateTime: function (time ){
Var myDate = new Date (time );
Return this. formatterDateTime (myDate );
},
/**
* Time Format to millisecond
*/
DateToLongMsTime: function (date ){
Var myDate = new Date (date );
Return myDate. getTime ();
},
/**
* Format the date (excluding the time)
*/
FormatterDate: function (date ){
Var datetime = date. getFullYear ()
+ "-" // "Year"
+ (Date. getMonth () + 1)> 10? (Date. getMonth () + 1): "0"
+ (Date. getMonth () + 1 ))
+ "-" // "Month"
+ (Date. getDate () <10? "0" + date. getDate (): date
. GetDate ());
Return datetime;
},
/**
* Format the date (including the time "00:00:00 ")
*/
FormatterDate2: function (date ){
Var datetime = date. getFullYear ()
+ "-" // "Year"
+ (Date. getMonth () + 1)> 10? (Date. getMonth () + 1): "0"
+ (Date. getMonth () + 1 ))
+ "-" // "Month"
+ (Date. getDate () <10? "0" + date. getDate (): date
. GetDate () + "" + "00:00:00 ";
Return datetime;
},
/**
* Format the date (including time)
*/
FormatterDateTime: function (date ){
Var datetime = date. getFullYear ()
+ "-" // "Year"
+ (Date. getMonth () + 1)> 10? (Date. getMonth () + 1): "0"
+ (Date. getMonth () + 1 ))
+ "-" // "Month"
+ (Date. getDate () <10? "0" + date. getDate (): date
. GetDate ())
+ ""
+ (Date. getHours () <10? "0" + date. getHours (): date
. GetHours ())
+ ":"
+ (Date. getMinutes () <10? "0" + date. getMinutes (): date
. GetMinutes ())
+ ":"
+ (Date. getSeconds () <10? "0" + date. getSeconds (): date
. GetSeconds ());
Return datetime;
},
/**
* Time Comparison {end time greater than start time}
*/
CompareDateEndTimeGTStartTime: function (startTime, endTime ){
Return (new Date (endTime. replace (/-/g, "/")> (new Date (
StartTime. replace (/-/g ,"/"))));
},
/**
* Verify that the start time is reasonable {the start time cannot be less than the current time {X} months}
*/
CompareRightStartTime: function (month, startTime ){
Var now = formatterDayAndTime (new Date ());
Var sms = new Date (startTime. replace (/-/g ,"/"));
Var EMS = new Date (now. replace (/-/g ,"/"));
Var tDayms = month * 30*24*60*60*1000;
Var dvalue = EMS-sms;
If (dvalue> tDayms ){
Return false;
}
Return true;
},
/**
* Verify the validity of the start time {the end time cannot be less than the current time {X} months}
*/
CompareRightEndTime: function (month, endTime ){
Var now = formatterDayAndTime (new Date ());
Var sms = new Date (now. replace (/-/g ,"/"));
Var EMS = new Date (endTime. replace (/-/g ,"/"));
Var tDayms = month * 30*24*60*60*1000;
Var dvalue = sms-EMS;
If (dvalue> tDayms ){
Return false;
}
Return true;
},
/**
* Verify the validity of the start time {the interval between the end time and the start time cannot be more than {X} months}
*/
CompareEndTimeGTStartTime: function (month, startTime, endTime ){
Var sms = new Date (startTime. replace (/-/g ,"/"));
Var EMS = new Date (endTime. replace (/-/g ,"/"));
Var tDayms = month * 30*24*60*60*1000;
Var dvalue = EMS-sms;
If (dvalue> tDayms ){
Return false;
}
Return true;
},
/**
* Obtain the [start time and end time values of the last few days, and calculate the time forward]
*/
GetRecentDaysDateTime: function (day ){
Var daymsTime = day * 24*60*60*1000;
Var yesterDatsmsTime = this. getCurrentMsTime ()-daymsTime;
Var startTime = this. longMsTimeConvertToDateTime (yesterDatsmsTime );
Var pastDate = this. formatterDate2 (new Date (startTime ));
Var nowDate = this. formatterDate2 (new Date ());
Var obj = {
StartTime: pastDate,
EndTime: nowDate
};
Return obj;
},
/**
* Get the value of today's [start time and end time]
*/
GetTodayDateTime: function (){
Var daymsTime = 24*60*60*1000;
Var tomorrowDatsmsTime = this. getCurrentMsTime () + daymsTime;
Var currentTime = this. longMsTimeConvertToDateTime (this. getCurrentMsTime ());
Var termorrowTime = this. longMsTimeConvertToDateTime (tomorrowDatsmsTime );
Var nowDate = this. formatterDate2 (new Date (currentTime ));
Var tomorrowDate = this. formatterDate2 (new Date (termorrowTime ));
Var obj = {
StartTime: nowDate,
EndTime: tomorrowDate
};
Return obj;
},
/**
* Obtain the value of tomorrow's [start time and end time]
*/
GetTomorrowDateTime: function (){
Var daymsTime = 24*60*60*1000;
Var tomorrowDatsmsTime = this. getCurrentMsTime () + daymsTime;
Var termorrowTime = this. longMsTimeConvertToDateTime (tomorrowDatsmsTime );
Var theDayAfterTomorrowDatsmsTime = this. getCurrentMsTime () + (2 * daymsTime );
Var theDayAfterTomorrowTime = this. longMsTimeConvertToDateTime (theDayAfterTomorrowDatsmsTime );
Var pastDate = this. formatterDate2 (new Date (termorrowTime ));
Var nowDate = this. formatterDate2 (new Date (theDayAfterTomorrowTime ));
Var obj = {
StartTime: pastDate,
EndTime: nowDate
};
Return obj;
}
};

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.