当前位置:网站首页>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
边栏推荐
- Realize a simple LAN communication (similar to feiqiu)
- Alipay certificate mode payment interface
- Problem: wechat developer tool visitor mode cannot use this function
- Data recovery software easyrecovery15 Download
- MFQE 2.0: A New Approach for Multi-FrameQuality Enhancement on Compressed Video
- PHP recursive multi-level classification, infinite classification
- Tencent two sides: @bean and @component are used on the same class. What happens?
- Rpm2rpm packaging steps
- 【BUUCTF】 EasySql
- Crypto questions
猜你喜欢
MFQE 2.0: A New Approach for Multi-FrameQuality Enhancement on Compressed Video
I love network security for new recruitment assessment
What network security problems are exposed when a large-scale QQ number theft event occurs?
Shell programming overview
Laravel upload error
Solve the error in my QT_ thread_ global_ End(): 3 threads didn't exit
QQ was stolen? The reason is
Why does the folder appear open in another program
go time. after
Thinkphp5 log file contains trick
随机推荐
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
Problems in QT creator (additional unknown and error lines are listed in the debug output window)
【Redis 系列】redis 学习十六,redis 字典(map) 及其核心编码结构
"As a service", the inevitable choice of enterprise digital transformation
Why is the resolution of the image generated by PHP GD library 96? How to change it to 72
Details of gets, fgetc, fgets, Getc, getchar, putc, fputc, putchar, puts, fputs functions
Learn about data kinship JSON format design from sqlflow JSON format
【科学文献计量】外文文献及中文文献关键词的挖掘与可视化
Go language for loop multivariable use
Attack and defense world web questions
[scientific research data processing] [basic] category variable frequency analysis chart, numerical variable distribution chart and normality test (including lognormal)
Logiciel de récupération de données easyrecovery15 téléchargement
Why does the folder appear open in another program
The programming competition is coming! B station surrounding, senior members and other good gifts to you!
Realize a simple LAN communication (similar to feiqiu)
@ResponseBody的作用
Crypto questions
Att & CK red team evaluation field (I)
Comprehensively analyze the basic features and summary of free and paid SSH tools
Laravel configures passport and returns token using JWT