博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JS 日期格式化
阅读量:4510 次
发布时间:2019-06-08

本文共 851 字,大约阅读时间需要 2 分钟。

//格式化CST日期的字串
function formatCSTDate(strDate, format) {
return formatDate(new Date(strDate), format);
}

//格式化日期,

function formatDate(date, format) {
var paddNum = function (num) {
num += "";
return num.replace(/^(\d)$/, "0$1");
}
//指定格式字符
var cfg = {
yyyy: date.getFullYear() //年 : 4位
, yy: date.getFullYear().toString().substring(2)//年 : 2位
, M: date.getMonth() + 1 //月 : 如果1位的时候不补0
, MM: paddNum(date.getMonth() + 1) //月 : 如果1位的时候补0
, d: date.getDate() //日 : 如果1位的时候不补0
, dd: paddNum(date.getDate())//日 : 如果1位的时候补0
, hh: date.getHours() //时
, mm: date.getMinutes() //分
, ss: date.getSeconds() //秒
}
format || (format = "yyyy-MM-dd hh:mm:ss");
return format.replace(/([a-z])(\1)*/ig, function (m) { return cfg[m]; });
}

 

调用:

        $("#CurrentDateTime").val(formatDate(new Date(), "yyyy-MM-dd hh:mm:ss"));

转载于:https://www.cnblogs.com/Michael-W/p/3823909.html

你可能感兴趣的文章
SQL查询语句
查看>>
python网络爬虫-使用Urllib
查看>>
iptables配置管理
查看>>
1.7 本机单步调试(Intellij IDEA)
查看>>
自动化运维:日志系统上线规范(十)
查看>>
同步、异步、阻塞与非阻塞
查看>>
01软件架构设计的思想与模式阅读笔记
查看>>
Selenium 上传文件失败,解决办法一
查看>>
2019年,我们需要加强关注网络安全的6大原因
查看>>
Hexo主题 —— NexT优化
查看>>
Python Web Flask源码解读(三)——模板渲染过程
查看>>
JavaScript 中的对象(一)- 对象的概念、模型、以及创建
查看>>
产品的痛点、爽点和痒点
查看>>
密码学摘要算法之SHA2
查看>>
dealloc和weak底层实现
查看>>
【网络】Windows 下 socket 编程范例
查看>>
【IT】CRC校验码是怎么回事呢?
查看>>
hashmap C++实现
查看>>
C++深拷贝和浅拷贝细节理解
查看>>
云风协程库coroutine源码分析
查看>>