【JS 日期】获取当前日期时间
Js 时钟
function timeGo() { setInterval(function () { var date = new Date(); var year = date.getFullYear(); //当前年份 var month = date.getMonth(); //当前月份 var data = date.getDate(); //天 var hours = date.getHours(); //小时 var minute = date.getMinutes(); //分 var second = date.getSeconds(); //秒 var day = date.getDay(); //获取当前星期几 // day = day == 0 ? "日" : day; var ampm = hours < 12 ? 'am' : 'pm'; $('#time').html(fnW(hours) + ":" + fnW(minute) + ":" + fnW(second)); $('#date').html('<span>' + year + '/' + (month + 1) + '/' + data + '</span><span>' + ampm + '</span><span>周' + getWeekUpper(day) + '</span>') }, 1000); function fnW(str) { var num; str >= 10 ? num = str : num = "0" + str; return num; } function getWeekUpper(day) { var res = ""; switch (day) { case 1: res = "一"; break; case 2: res = "二"; break; case 3: res = "三"; break; case 4: res = "四"; break; case 5: res = "五"; break; case 6: res = "六"; break; case 0: res = "一"; break; } return res; } }
function getNowTime() { var date = new Date(); this.year = date.getFullYear(); this.month = date.getMonth() + 1; this.date = date.getDate(); this.hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(); this.minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(); this.second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds(); this.milliSeconds = date.getMilliseconds(); var currentTime = this.year + '-' + this.month + '-' + this.date + ' ' + this.hour + ':' + this.minute + ':' + this.second + '.' + this.milliSeconds; return currentTime; }
获取当前日期时间
<!DOCTYPE html> <html> <head></head> <body> <script> window.onload = function () { getDateTime(); } function getDateTime() { console.log(getLangDate()); setTimeout("getDateTime()", 1000); } function getLangDate() { var dateObj = new Date(); //表示当前系统时间的Date对象 var year = dateObj.getFullYear(); //当前系统时间的完整年份值 var month = dateObj.getMonth() + 1; //当前系统时间的月份值 var date = dateObj.getDate(); //当前系统时间的月份中的日 var day = dateObj.getDay(); //当前系统时间中的星期值 var weeks = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]; var week = weeks[day]; //根据星期值,从数组中获取对应的星期字符串 var hour = dateObj.getHours(); //当前系统时间的小时值 var minute = dateObj.getMinutes(); //当前系统时间的分钟值 var second = dateObj.getSeconds(); //当前系统时间的秒钟值 var timeValue = "" + ((hour >= 12) ? (hour >= 18) ? "晚上" : "下午" : "上午" ); //当前时间属于上午、晚上还是下午 var newDate = dateFilter(year) + "-" + dateFilter(month) + "-" + dateFilter(date) + " " + dateFilter(hour) + ":" + dateFilter(minute) + ":" + dateFilter(second); return newDate; } function dateFilter(date) { return date < 10 ? ("0" + date) : date; } </script> </body> </html>