jQuery获取格式化当前时间
来源:互联网 发布:中国网络资讯台无锡市 编辑:程序博客网 时间:2024/05/18 02:33
一)
重构Date对象:
// 对Date的扩展,将 Date 转化为指定格式的String // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
Date.prototype.Format = function (fmt) { var o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "H+": this.getHours(), //小时 "m+": this.getMinutes(), //分 "s+": this.getSeconds(), //秒 "q+": Math.floor((this.getMonth() + 3) / 3), //季度 "S": this.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); return fmt; }
调用:
var nowTime=new Date().Format("yyyy-MM-dd"); console.log(nowTime);
二)
格式一:(yyyy-MM-dd HH:mm:SS)
function getFormatDate(){ var nowDate = new Date(); var year = nowDate.getFullYear(); var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1; var date = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate.getDate(); var hour = nowDate.getHours()< 10 ? "0" + nowDate.getHours() : nowDate.getHours(); var minute = nowDate.getMinutes()< 10 ? "0" + nowDate.getMinutes() : nowDate.getMinutes(); var second = nowDate.getSeconds()< 10 ? "0" + nowDate.getSeconds() : nowDate.getSeconds(); return year + "-" + month + "-" + date+" "+hour+":"+minute+":"+second; }var str = getFormatDate();console.log(str);
格式二:(yyyy-MM-dd)
function getFormatDate(){ var nowDate = new Date(); var year = nowDate.getFullYear(); var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1; var date = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate.getDate(); // var hour = nowDate.getHours()< 10 ? "0" + nowDate.getHours() : nowDate.getHours(); // var minute = nowDate.getMinutes()< 10 ? "0" + nowDate.getMinutes() : nowDate.getMinutes(); // var second = nowDate.getSeconds()< 10 ? "0" + nowDate.getSeconds() : nowDate.getSeconds(); return year + "-" + month + "-" + date; } var str = getFormatDate();console.log(str);
阅读全文
4 0
- jQuery获取格式化当前时间
- 获取当前时间并格式化
- java 获取当前格式化时间
- 获取当前时间及格式化
- jquery获取当前时间
- jquery获取当前时间
- jquery 获取当前时间
- jquery获取当前时间
- jquery获取当前时间
- jquery获取当前时间
- jQuery获取当前时间
- jquery 获取当前时间
- jquery获取当前时间
- jquery获取当前时间
- mysql获取当前时间,格式化当前时间,增减当前时间
- java 时间 格式化(获取当前时间)
- VC获取当前时间,时间格式化详解
- js 时间格式化 获取当前时间
- const与#define
- linux下源码包安装apache
- Atmel Studio-SAM单片机开发 ---EXTINT
- 关于RecyclerView列表点击事件ripple效果失效的问题
- 深搜 加奇偶剪枝
- jQuery获取格式化当前时间
- Kotlin Reference (三) Coding Conventions
- 雷军给程序员的五点建议--如何成为编程高手并以此创业
- [Muduo网络库源码分析] (5) base/FileUtil.cc_h_文件操作
- Scala定义函数的5种方式
- 有关数组的基础递归
- 改变this指向(call、apply、bind)
- cannot simultaneously fetch multiple bags[]不能抓取一对多的属性
- iOS pop到指定页面