This article mainly introduces Javascript to get the current time function and Time Operation summary. This article summarizes based on the actual needs of the project. If you need it, you can refer to the need for a timer in the project. The effect is as follows:
Js Code:
The Code is as follows:
/* Get the current time */
Function getCurrentDate ()
{
Var timeStr = '';
Var curDate = new Date ();
Var curMonth = curDate. getMonth () + 1; // obtain the current month (0-11, 0 indicates January)
Var curDay = curDate. getDate (); // obtain the current day (1-31)
Var curWeekDay = curDate. getDay (); // obtain X of the current week (0-6, 0 indicates Sunday)
Var curHour = curDate. getHours (); // obtain the current hour (0-23)
Var curMinute = curDate. getMinutes (); // obtain the current number of minutes (0-59)
Var curSec = curDate. getSeconds (); // obtain the current number of seconds (0-59)
TimeStr = curMonth + 'month' + curDay + 'day Week ';
Switch (curWeekDay)
{
Case 0: timeStr + = 'day'; break;
Case 1: timeStr + = '1'; break;
Case 2: timeStr + = '2'; break;
Case 3: timeStr + = '3'; break;
Case 4: timeStr + = '4'; break;
Case 5: timeStr + = '5'; break;
Case 6: timeStr + = '6'; break;
}
If (curHour <10)
{
If (curMinute <10)
{
If (curSec <10)
{
TimeStr + = '0' + curHour + ': 0' + curMinute +': 0' + curSec;
}
Else
{
TimeStr + = '0' + curHour + ': 0' + curMinute +': '+ curSec;
}
}
Else
{
If (curSec <10)
{
TimeStr + = '0' + curHour + ':' + curMinute + ': 0' + curSec;
}
Else
{
TimeStr + = '0' + curHour + ':' + curMinute + ':' + curSec;
}
}
}
Else
{
If (curMinute <10)
{
If (curSec <10)
{
TimeStr + = ''+ curHour + ': 0' + curMinute +': 0' + curSec;
}
Else
{
TimeStr + = ''+ curHour + ': 0' + curMinute +': '+ curSec;
}
}
Else
{
If (curSec <10)
{
TimeStr + = ''+ curHour + ':' + curMinute + ': 0' + curSec;
}
Else
{
TimeStr + = ''+ curHour + ':' + curMinute + ':' + curSec;
}
}
}
$ ("# Time"). text (timeStr );
}
Then use this function.
Finally, we will summarize some functions of the Javascript Date:
Var myDate = new Date ();
MyDate. getYear (); // obtain the current year (2 digits)
MyDate. getFullYear (); // obtain the complete year (4 bits, 1970 -????)
MyDate. getMonth (); // obtain the current month (0-11, 0 represents January)
MyDate. getDate (); // obtain the current day (1-31)
MyDate. getDay (); // obtain X of the current week (0-6, 0 indicates Sunday)
MyDate. getTime (); // obtain the current time (milliseconds starting from 1970.1.1)
MyDate. getHours (); // obtain the current hour (0-23)
MyDate. getMinutes (); // obtain the current number of minutes (0-59)
MyDate. getSeconds (); // obtain the current number of seconds (0-59)
MyDate. getMilliseconds (); // obtain the current number of milliseconds (0-999)
MyDate. toLocaleDateString (); // obtain the current date
Var mytime = myDate. toLocaleTimeString (); // obtain the current time
MyDate. toLocaleString (); // obtain the date and time