jquery获取前一个月日期
2024-08-24 17:02:40
一)
重构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);
最新文章
- 腾讯开放平台 手机QQ登录 错误码:110406 解决办法
- adb devices 偵測不到 手機
- 浏览器兼容性小记-DOM篇(二)
- java.注释类型
- 例题:for循环迭代法。一个棋盘有n个格子,第一个格子有一粒米,第二个格子有两粒米,第三个格子有四粒米,依次类推,第n个格子里有多少粒米,棋盘里一共有多少粒米。
- 批量缩放PNG图片.
- ZOJ3870 Team Formation
- wpf下拉框不能多选的原因
- JavaEE Tutorials (27) - Java EE的并发工具
- An update on OS X Code Signing(OS X代码签名)
- ImageMagick图片服务器
- C语言数据类型的转换(隐式转换)
- C# test
- 分布式任务调度系统xxl-job搭建
- BUPT2017 wintertraining(15) #3 题解
- centos6.5环境disconf管理端安装配置详解
- 力扣(LeetCode)412. Fizz Buzz
- Coding kata: get the top two teams in one group
- Laravel应用性能调优
- PDF文档输出