A timer is required in the project, and the effect is as follows:
js code:
The code copy is as follows:
/*Get the current time*/
function getCurrentDate()
{
var timeStr = '';
var curDate = new Date();
var curMonth = curDate.getMonth()+1; //Get the current month (0-11, 0 represents January)
var curDay = curDate.getDate(); //Get the current day (1-31)
var curWeekDay = curDate.getDay(); //Get the current week X (0-6, 0 represents Sunday)
var curHour = curDate.getHours(); //Get the current number of hours (0-23)
var curMinute = curDate.getMinutes(); // Get the current number of minutes (0-59)
var curSec =curDate.getSeconds(); //Get the current number of seconds (0-59)
timeStr = curMonth+'month'+curDay+'day and week';
switch(curWeekDay)
{
case 0:timeStr += 'day';break;
case 1:timeStr += 'one';break;
case 2:timeStr += 'two';break;
case 3:timeStr += 'three';break;
case 4:timeStr += 'four';break;
case 5:timeStr += 'five';break;
case 6:timeStr += 'six';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, let’s summarize some functions of Javascript date:
var myDate = new Date();
myDate.getYear(); //Get the current year (2 digits)
myDate.getFullYear(); //Get the full year (4 digits, 1970-????)
myDate.getMonth(); //Get the current month (0-11, 0 represents January)
myDate.getDate(); //Get the current day (1-31)
myDate.getDay(); //Get the current week X (0-6, 0 represents Sunday)
myDate.getTime(); //Get the current time (number of milliseconds starting from 1970.1.1)
myDate.getHours(); //Get the current number of hours (0-23)
myDate.getMinutes(); //Get the current number of minutes (0-59)
myDate.getSeconds(); //Get the current number of seconds (0-59)
myDate.getMilliseconds(); //Get the current number of milliseconds (0-999)
myDate.toLocaleDateString(); //Get the current date
var mytime=myDate.toLocaleTimeString(); //Get the current time
myDate.toLocaleString( ); //Get date and time