Summary of Javascript time operations
Js Code
1:/* Get the current time */
2: function getCurrentDate()
3: {
4: var timeStr = '';
5: var curDate = new Date();
6: var curMonth = curDate. getMonth () + 1; // obtain the current month (0-11, 0 represents January)
7: var curDay = curDate. getDate (); // obtain the current day (1-31)
8: var curWeekDay = curDate. getDay (); // obtain X of the current week (0-6, 0 indicates Sunday)
9: var curHour = curDate. getHours (); // obtain the current hour (0-23)
10: var curMinute = curDate. getMinutes (); // obtain the current number of minutes (0-59)
11: var curSec = curDate. getSeconds (); // obtain the current number of seconds (0-59)
12: timeStr = curMonth + 'month' + curDay + 'day Week ';
13: switch(curWeekDay)
14: {
15: case 0: timeStr + = 'day'; break;
16: case 1: timeStr + = '1'; break;
17: case 2: timeStr + = '2'; break;
18: case 3: timeStr + = '3'; break;
19: case 4: timeStr + = '4'; break;
20: case 5: timeStr + = '5'; break;
21: case 6: timeStr + = '6'; break;
22: }
23: if(curHour < 10)
24: {
25: if(curMinute < 10)
26: {
27: if(curSec < 10)
28: {
29: timeStr += ' 0'+curHour+':0'+curMinute+':0'+curSec;
30: }
31: else
32: {
33: timeStr += ' 0'+curHour+':0'+curMinute+':'+curSec;
34: }
35: }
36: else
37: {
38: if(curSec < 10)
39: {
40: timeStr += ' 0'+curHour+':'+curMinute+':0'+curSec;
41: }
42: else
43: {
44: timeStr += ' 0'+curHour+':'+curMinute+':'+curSec;
45: }
46: }
47: }
48: else
49: {
50: if(curMinute < 10)
51: {
52: if(curSec < 10)
53: {
54: timeStr += ' '+curHour+':0'+curMinute+':0'+curSec;
55: }
56: else
57: {
58: timeStr += ' '+curHour+':0'+curMinute+':'+curSec;
59: }
60: }
61: else
62: {
63: if(curSec < 10)
64: {
65: timeStr += ' '+curHour+':'+curMinute+':0'+curSec;
66: }
67: else
68: {
69: timeStr += ' '+curHour+':'+curMinute+':'+curSec;
70: }
71: }
72: }
73: $(#time).text(timeStr);
74: }
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