< 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>