当前位置:网站首页>JS日期格式化转换方法
JS日期格式化转换方法
2022-07-01 11:11:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
1. 将日期转换为指定的格式:比如转换成 年月日时分秒 这种格式:yyyy-MM-dd hh:mm:ss 或者 yyyy-MM-dd。当然是网上的方法,只是总结下。
可以为Date原型添加如下的方法:
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 time1 = new Date().format(“yyyy-MM-dd hh:mm:ss”); console.log(time1);
运行如下:
也可以转换成 ”年月日”的格式
var time2 = new Date().format(“yyyy-MM-dd”); console.log(time2);
运行如下:
2. 将指定的日期转换为”年月日”的格式,代码如下:
var oldTime = (new Date(“2012/12/25 20:11:11”)).getTime(); var curTime = new Date(oldTime).format(“yyyy-MM-dd”); console.log(curTime);
运行如下:
3. 将 “时间戳” 转换为 “年月日” 的格式.
比如如下代码:
var da = 1402233166999; da = new Date(da); var year = da.getFullYear()+’年’; var month = da.getMonth()+1+’月’; var date = da.getDate()+’日’; console.log([year,month,date].join(‘-‘));
运行如下:
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/131628.html原文链接:https://javaforall.cn
边栏推荐
- Flip the array gracefully
- Huawei equipment is configured with large network WLAN basic services
- Cvpr22 | CMT: efficient combination of CNN and transformer (open source)
- CVPR 2022 | self enhanced unpaired image defogging based on density and depth decomposition
- LeetCode.515. 在每个树行中找最大值___逐一BFS+DFS+按层BFS
- Ask everyone in the group about the fact that the logminer scheme of flick Oracle CDC has been used to run stably in production
- CPI教程-异步接口创建及使用
- Whether lending a bank card to others constitutes a crime
- Yoda unified data application -- Exploration and practice of fusion computing in ant risk scenarios
- 索引失效的几种情况
猜你喜欢

Intel Labs annonce de nouveaux progrès en photonique intégrée

TEMPEST HDMI泄漏接收 4

英特尔实验室公布集成光子学研究新进展

Oracle和JSON的結合

NC | intestinal cells and lactic acid bacteria work together to prevent Candida infection

12 product management platforms that everyone is using

商城小程序源码开源版-可二开

NeurIPS 2022 | 细胞图像分割竞赛正式启动!

Combinaison Oracle et json

关于Keil编译程序出现“File has been changed outside the editor,reload?”的解决方法
随机推荐
TEMPEST HDMI泄漏接收 4
Guys, how to export iceberg data to MySQL? What tools are there? Neither sqoop nor dataX
CVPR 2022 | Virtual Correspondence: Humans as a Cue for Extreme-View Geometry
tmux使用
Combination of Oracle and JSON
The list of winners of the digital collection of "century master" was announced
Cvpr22 | CMT: efficient combination of CNN and transformer (open source)
Dameng data rushes to the scientific innovation board: it plans to raise 2.4 billion yuan. Feng Yucai was once a professor of Huake
Network security learning notes 01 network security foundation
Global filter (processing time format)
开发说,“ 这个不用测,回归正常流程就行 “,测试人员怎么办?
[.net6] use ml.net+onnx pre training model to liven the classic "Huaqiang buys melons" in station B
提问:测试工程师应该具备哪些职业素养?
Harbor webhook从原理到构建
转义字符串
Numpy的矩阵
编译调试Net6源码
JS foundation -- data type
sdp 协议中的packetization-mode方式和三种流传输模式
[AI information monthly] 350 + resources! All the information and trends that can't be missed in June are here! < Download attached >