当前位置:网站首页>JS date format conversion method
JS date format conversion method
2022-07-01 11:19:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
1. Convert the date to the specified format : Like converting to Mm / DD / yyyy HHM / S This format :yyyy-MM-dd hh:mm:ss perhaps yyyy-MM-dd. Of course, online methods , Just to sum up .
It can be for Date The prototype adds the following methods :
Date.prototype.format = function(fmt) {
var o = {
"M+" : this.getMonth()+1, // month
"d+" : this.getDate(), // Japan
"h+" : this.getHours(), // Hours
"m+" : this.getMinutes(), // branch
"s+" : this.getSeconds(), // second
"q+" : Math.floor((this.getMonth()+3)/3), // quarter
"S" : this.getMilliseconds() // millisecond
};
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;
} For example, we can call :
var time1 = new Date().format(“yyyy-MM-dd hh:mm:ss”); console.log(time1);
Run the following :
It can also be converted into ” Specific date ” The format of
var time2 = new Date().format(“yyyy-MM-dd”); console.log(time2);
Run the following :
2. Convert the specified date to ” Specific date ” The format of , The code is as follows :
var oldTime = (new Date(“2012/12/25 20:11:11”)).getTime(); var curTime = new Date(oldTime).format(“yyyy-MM-dd”); console.log(curTime);
Run the following :
3. take “ Time stamp ” Convert to “ Specific date ” The format of .
For example, the following code :
var da = 1402233166999; da = new Date(da); var year = da.getFullYear()+’ year ’; var month = da.getMonth()+1+’ month ’; var date = da.getDate()+’ Japan ’; console.log([year,month,date].join(‘-‘));
Run the following :
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/131628.html Link to the original text :https://javaforall.cn
边栏推荐
- kubernetes之ingress探索实践
- Ultra detailed black apple installation graphic tutorial sent to EFI configuration collection and system
- Google's new paper Minerva: solving quantitative reasoning problems with language models
- Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
- Huawei Equipment configure les services de base du réseau WLAN à grande échelle
- MySQL in and not in() empty list error
- 分享psd格式怎么预览的方法和psd文件缩略图插件[通俗易懂]
- sshd_config 中 PermitRootLogin 的探讨
- Leetcode 181 Employees exceeding the manager's income (June 29, 2022)
- Ten years of sharpening a sword: unveiling the secrets of ant group's observability platform antmonitor
猜你喜欢

Several cases of index failure

Matrix of numpy

Mall applet source code open source version - two open

Tempest HDMI leak receive 5

Node version manager NVM installation and switching

2022/6/29学习总结

Cvpr22 | CMT: efficient combination of CNN and transformer (open source)

Personal mall two open Xiaoyao B2C mall system source code - Commercial Version / group shopping discount seckill source code

2022年6月编程语言排行,第一名居然是它?!

英特爾實驗室公布集成光子學研究新進展
随机推荐
Introduction to unittest framework and the first demo
The developer said, "this doesn't need to be tested, just return to the normal process". What about the testers?
力扣(LeetCode)181. 超过经理收入的员工(2022.06.29)
编译调试Net6源码
Oracle和JSON的結合
达梦数据冲刺科创板:拟募资24亿 冯裕才曾为华科教授
提问:测试工程师应该具备哪些职业素养?
LeetCode. 515. Find the maximum value in each tree row___ BFS + DFS + BFS by layer
How to realize the four isolation levels of MySQL (brief)
转义字符串
Can servers bundled with flask be safely used in production- Is the server bundled with Flask safe to use in production?
Can I open a securities account anywhere? Is it safe to open an account
8 best practices to protect your IAC security!
flutter path_ Provider: ^2.0.10 can get temporary directory
"Target detection" + "visual understanding" to realize the understanding and translation of the input image (with source code)
证券账户销户后果 开户安全吗
Huawei equipment is configured with large network WLAN basic services
applyMiddleware 原理
What are the advantages and disadvantages of PHP
Jd.com renewed its cooperation with Tencent: issuing class A shares to Tencent with a maximum value of US $220million