Get date interval in Javascript

Source: Internet
Author: User
< Script Type = " Text/JavaScript " Language = " Javascript " >
Function Getdatetime ()
{
VaR Vbegindate = Document. All [ " Txbintendbegin " ]. Value;
VaR Venddate = Document. All [ " Txbintendend " ]. Value;
If (Vbegindate ! = Null   && Venddate ! = Null )
{
VaR Dbegin = Vbegindate. Split ( " . " );
VaR Dend = Venddate. Split ( " . " );
VaR Dtbegin = New Date (dbegin [ 0 ], Dbegin [ 1 ], Dbegin [ 2 ]);
VaR Dtend = New Date (dend [ 0 ], Dend [ 1 ], Dend [ 2 ]);
// Get the number of days. Remove 24 to get the hour interval.
VaR Timespan = (Dtend - Dtbegin) / (24*60*60*1000 );
If (Timespan < 0 )
{
// Check
Alert ( " The end date must be later than the start date! " );
Document. All [ " Txbintendend " ]. Focus ();
Return   False ;
}
Else
{
VaR Vworkload = Document. getelementbyid ( " Txbintendworkload " );
If (Vworkload ! = Null )
{
// Display interval (days) on the page)
Vworkload. Value = Timespan. tostring ();
}
}
}
Else
{
Window. Alert ( " No information " );
Return   False ;
}
Return   True ;

}

 

There are other methods on the internet,CodeAs follows:

< Script Language = " Javascript " >
<! --

// Note: The math. Ceil () function is used here. It is rounded up, that is, the first day is counted as the first day.
VaR Strdate1 = " 03:03:40. 0 " ;
VaR Strdate2 = " 12:03:12. 0 " ;
Strdate1 = Strdate1.substring ( 0 , Strdate1.lastindexof ( " . " ). Replace ( / - / G, " / " );
Strdate2 = Strdate2.substring ( 0 , Strdate2.lastindexof ( " . " ). Replace ( / - / G, " / " );
VaR Date1 = Date. parse (strdate1 );
VaR Date2 = Date. parse (strdate2 );
Alert ( " Difference between strdate2 and strdate1 " + Math. Ceil (date2 - Date1) / (24*60*60*1000) + "day ")
// -->

</SCRIPT>
 
<Script Language="Javascript">

<! --
VaR Strdate1 = " 03:03:00. 0 " ;
VaR Strdate2 = " 04:05:00. 0 " ;
Strdate1 = Strdate1.substring ( 0 , Strdate1.lastindexof ( " . " ). Replace ( / - / G, " / " );
Strdate2 = Strdate2.substring ( 0 , Strdate2.lastindexof ( " . " ). Replace ( / - / G, " / " );
VaR Date1 = Date. parse (strdate1 );
VaR Date2 = Date. parse (strdate2 );
Alert ( " Difference between strdate2 and strdate1 " + (Date2 - Date1) / (60x60x1000) + "Hour ")
// -->

</SCRIPT>

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.