1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8" /> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0" /> 6 <title>new Date() 日期格式处理</title> 7 </head> 8 <body> 9 <script> 10 var myDate = new Date(); 11 12 var y = myDate.getYear(); 13 console.log("年份", y); 14 var fullYear = myDate.getFullYear(); 15 console.log("完整年份", fullYear); 16 var m = myDate.getMonth() + 1; 17 console.log("月份", m); 18 var d = myDate.getDate(); 19 console.log("日期", d); 20 var day = myDate.getDay(); 21 console.log("星期", day); 22 var time = myDate.getTime(); 23 console.log("获取当前时间(从1970.1.1开始的毫秒数)", time); 24 var h = myDate.getHours(); 25 console.log("获取当前小时数(0-23)", h); 26 var min = myDate.getMinutes(); 27 console.log("获取当前分钟数(0-59) ", min); 28 var sec = myDate.getSeconds(); 29 console.log("获取当前秒数(0-59):", sec); 30 var haosec = myDate.getMilliseconds(); 31 console.log("获取当前毫秒数(0-999)", haosec); 32 var riqi = myDate.toLocaleDateString(); 33 console.log("获取当前日期 ", riqi); 34 var mytime = myDate.toLocaleTimeString(); 35 console.log("获取当前时间", mytime); 36 var dt = myDate.toLocaleString(); 37 console.log("获取当前日期和时间", dt); 38 </script> 39 <script> 40 //JS获取当前时间戳的方法-JavaScript 获取当前毫秒时间戳有以下三种方法: 41 var timestamp = Date.parse(new Date()); //不推荐 42 console.log(timestamp); 43 44 var timestamp2 = new Date().valueOf(); 45 console.log(timestamp2); 46 47 var timestamp3 = new Date().getTime(); 48 console.log(timestamp3); 49 50 var ti = new Date(); 51 console.log(ti); // Mon Nov 23 2020 15:17:09 GMT+0800 (中国标准时间) 52 </script> 53 <script> 54 var strTime = "2011-11-23"; 55 var da = strTime.replace(/-/g, "/"); 56 console.log(da); //2011/11/23 57 var date = new Date(Date.parse(strTime.replace(/-/g, "/"))); 58 console.log("字符串日期格式:", date); 59 </script> 60 61 <script> 62 function add0(m) { 63 return m < 10 ? "0" + m : m; 64 } 65 function format(shijianchuo) { 66 //shijianchuo是整数,否则要parseInt转换 67 var time = new Date(shijianchuo); 68 var y = time.getFullYear(); 69 var m = time.getMonth() + 1; 70 var d = time.getDate(); 71 var h = time.getHours(); 72 var mm = time.getMinutes(); 73 var s = time.getSeconds(); 74 return ( 75 y + 76 "-" + 77 add0(m) + 78 "-" + 79 add0(d) + 80 " " + 81 add0(h) + 82 ":" + 83 add0(mm) + 84 ":" + 85 add0(s) 86 ); 87 } 88 console.log("时间戳转为日期格式1:", format(1606118452913)); //2020-11-23 16:00:52 89 90 var oldTime = new Date("2015/06/23 08:00:20").getTime() / 1000; 91 var oldTime2 = new Date("1997/01/01 08:00:20").getTime() / 1000; 92 console.log("时间戳转为日期格式2", oldTime); //1435017620 93 console.log("时间戳转为日期格式2", oldTime2); //852076820 94 </script> 95 96 <script> 97 var parserDate = function (date) { 98 var t = Date.parse(date); 99 if (!isNaN(t)) { 100 return new Date(Date.parse(date.replace(/-/g, "/"))); 101 } else { 102 return new Date(); 103 } 104 }; 105 console.log( 106 "如何将2015-03-12 12:00 转换成标准时间:", 107 parserDate("2015-03-19 12:00:00") 108 ); 109 110 var time1 = "2016-01-01 17:22:37"; 111 var date = new Date(time1.replace(/-/g, "/")); //开始时间 112 var time2 = date.getTime(); 113 console.log("时间格式字符串转为时间戳(毫秒):", time2); 114 </script> 115 </body> 116 </html>