當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
JavaScript获取当前日期时间
生活随笔
收集整理的這篇文章主要介紹了
JavaScript获取当前日期时间
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
獲取當前日期時間
// 獲取當前日期時間 var myDate = new Date();myDate.toLocaleDateString(); //獲取當前日期 var mytime=myDate.toLocaleTimeString(); //獲取當前時間 myDate.toLocaleString( ); //獲取日期與時間myDate.getYear(); //獲取當前年份(2位) myDate.getFullYear(); //獲取完整的年份(4位,1970-????) myDate.getMonth(); //獲取當前月份(0-11,0代表1月) myDate.getDate(); //獲取當前日(1-31) myDate.getDay(); //獲取當前星期X(0-6,0代表星期天) myDate.getTime(); //獲取當前時間(從1970.1.1開始的毫秒數) myDate.getHours(); //獲取當前小時數(0-23) myDate.getMinutes(); //獲取當前分鐘數(0-59) myDate.getSeconds(); //獲取當前秒數(0-59) myDate.getMilliseconds(); //獲取當前毫秒數(0-999)// 獲取當前日期時間 function getDatetime() {var now = new Date();var year = now.getFullYear(); var month = now.getMonth() + 1; var day = now.getDate(); var hh = now.getHours(); var mm = now.getMinutes(); var ss = now.getSeconds(); var clock = year + "-";if (month < 10)clock += "0";clock += month + "-";if (day < 10)clock += "0";clock += day + " ";if (hh < 10)clock += "0";clock += hh + ":";if (mm < 10) clock += '0';clock += mm + ":";if (ss < 10) clock += '0';clock += ss;return clock; }// 獲取當前日期時間 function timestampToTime(timestamp) {var date = new Date(timestamp);var Y = date.getFullYear() + '-';var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate() + ' ';var hh = date.getHours() < 10 ? '0' + date.getHours() : date.getHours() + ':';var mm = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes() + ':';var ss = date.getSeconds() < 10 ? '0' + date.getDate() : date.getSeconds() ;return Y + M + D + hh + mm + ss; }總結
以上是生活随笔為你收集整理的JavaScript获取当前日期时间的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WdatePicker控件的使用
- 下一篇: JSTL标签显示分页