获取当前日期时间并格式化 - 常见格式
生活随笔
收集整理的這篇文章主要介紹了
获取当前日期时间并格式化 - 常见格式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
獲取系統當前日期時間的方法: 1 //獲取系統當前日期時間
2 Date.prototype.format = function (format) {
3 var o = {
4 "M+": this.getMonth() + 1, //month
5 "d+": this.getDate(), //day
6 "h+": this.getHours(), //hour
7 "m+": this.getMinutes(), //minute
8 "s+": this.getSeconds(), //second
9 "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
10 "S": this.getMilliseconds() //millisecond
11 };
12
13 if (/(y+)/.test(format)) {
14 format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
15 }
16
17 for (var k in o) {
18 if (new RegExp("(" + k + ")").test(format)) {
19 format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
20 }
21 }
22 return format;
23 }
使用方法1:
var now = new Date(); var nowStr = now.format("yyyy-MM-dd hh:mm:ss");使用方法2:
var testDate = new Date(); var testStr = testDate.format("yyyy年MM月dd日hh小時mm分ss秒"); console.log(nowStr); console.log(testStr);使用方法3:
console.log(new Date().format("yyyy年MM月dd日")); console.log(new Date().format("MM/dd/yyyy")); console.log(new Date().format("yyyyMMdd")); console.log(new Date().format("yyyy-MM-dd hh:mm:ss")); light7 框架使用日期時間模塊,對其限制時間做小為當前時間: $(function () {$(document).on("pageInit", function () {$('input[name="newSignDate"]').datetimePicker({toolbarTemplate: '<header class="bar bar-nav">\<button class="button button-link pull-right close-picker">確定</button>\<h1 class="title">選擇日期和時間</h1>\</header>'}).val(function () {return new Date().format("yyyy-MM-dd hh:mm:ss");}).bind('change', function () {var _date = new Date().format("yyyy-MM-dd hh:mm:ss");console.log($(this).val());if ($(this).val() <= _date) {$.alert("簽收時間必須大于當前時間")}})});$.init(); })轉載于:https://www.cnblogs.com/myclovers/p/5394722.html
總結
以上是生活随笔為你收集整理的获取当前日期时间并格式化 - 常见格式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大年三十整理的asp.net资料!(不得
- 下一篇: 应用开发框架之——数据字典、原数据、模板