当前位置:网站首页>JS time conversion standard format, timestamp conversion standard format
JS time conversion standard format, timestamp conversion standard format
2022-06-30 14:28:00 【Bamboo shrimp】
The front end often needs to use a time format of :
2020-08-10 10:00:34
I put together a generic function
Now go straight to the code , You can use it :
One 、 Convert current time to standard format :
function getFormatDate() {
var date = new Date();
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
month = (month<10)? '0'+ month : month;
day = (day<10)? '0'+ day : day;
hour = (hour<10)? '0'+ hour : hour;
minutes = (minutes<10)? '0'+ minutes : minutes;
seconds = (seconds<10)? '0'+ seconds : seconds;
var currentDate = date.getFullYear() + "-" + month + "-" + day
+ " " + hour + ":" + minutes + ":" + seconds;
return currentDate;
}
console.log(getFormatDate()) //2020-08-10 10:00:34
This new Date() In fact, it is the time taken from the computer system , If you find that the time you get does not match the real time , Remember to adjust the system time to match the network time
Two 、 Timestamp conversion to standard format :
This scenario is often used when the data returned from the back end is timestamp , The front-end manual conversion processing is required
function formatDate(datetime) {
var date = new Date(datetime); //datetime Timestamp 13 Bit millisecond level , If 10 Bit multiplication 1000
var month = ("0" + (date.getMonth() + 1)).slice(-2),
sdate = ("0" + date.getDate()).slice(-2),
hour = ("0" + date.getHours()).slice(-2),
minute = ("0" + date.getMinutes()).slice(-2),
second = ("0" + date.getSeconds()).slice(-2);
var thatDate = date.getFullYear() + "-" + month + "-" + sdate + " " + hour + ":" + minute + ":" + second;
// return
return thatDate;
}
console.log(formatDate(1599085447000)) //2020-09-03 06:24:07
If what is needed is not ‘2020-09-03 06:24:07’ It is ‘2020 year 09 month 03 Japan 06:24:07’, Just change the concatenated string
边栏推荐
- 【观察】智能产业加速,为何AI算力要先行?
- 表格储存中sql查询的时候,查询结果增加主键报错,查询结果超过10w行。需要对主键增加上多元索引吗?
- 數據恢複軟件EasyRecovery15下載
- Heavyweight: the domestic ide was released, developed by Alibaba, and is completely open source!
- 深入理解.Net中的线程同步之构造模式(二)内核模式4.内核模式构造物的总结
- ThinkPHP show method parameter controllable command execution
- Detailed explanation of the first three passes of upload Labs
- Rpm2rpm packaging steps
- org. json. The jsonobject object is converted to JSON, and JSON adds new elements. The value is obtained according to the JSON key. And list object format string to jsonarray
- Go language for loop multivariable use
猜你喜欢

About the problems encountered when using the timer class to stop with a button (why does the QPushButton (for the first time) need to be clicked twice to respond?)

More than 20 years after Hong Kong's return, Tupu digital twin Hong Kong Zhuhai Macao Bridge has shocked

提权扫描工具

编程实战赛来啦!B站周边、高级会员等好礼送你啦!

QQ 居然被盗了?原因在这......
![[geek challenge 2019] PHP problem solving record](/img/bf/038082e8ee1c91eaf6e35add39f760.jpg)
[geek challenge 2019] PHP problem solving record

The programming competition is coming! B station surrounding, senior members and other good gifts to you!

Race of golang

I love network security for new recruitment assessment

Att & CK red team evaluation field (I)
随机推荐
Mutex lock, read / write lock, spin lock, pessimistic lock, and optimistic lock
Jetpack compose for perfect screen fit
@Component use cases
Why does the folder appear open in another program
Optimization of unit test efficiency: why test programs? What are the benefits of testing?
Configuration of headquarters dual computer hot standby and branch infrastructure for firewall Foundation
PHP generate images into Base64
@ResponseBody的作用
Why is the resolution of the image generated by PHP GD library 96? How to change it to 72
[scientific research data processing] [practice] frequency analysis chart of category variables, distribution chart of numerical variables and normality test (including lognormal)
Defi "where does the money come from"? A problem that most people don't understand
More than 20 years after Hong Kong's return, Tupu digital twin Hong Kong Zhuhai Macao Bridge has shocked
The JSON data returned from the control layer to JS has a "\" translator. How to remove it
DiceCTF - knock-knock
【BUUCTF】[GXYCTF2019]Ping Ping Ping1
Implementation of forwarding server using IO multiplexing
Go language func function
Laravel upload error
Go common lock mutex and rwmutex
org. json. The jsonobject object is converted to JSON, and JSON adds new elements. The value is obtained according to the JSON key. And list object format string to jsonarray