JS時間轉(zhuǎn)換標準格式、時間戳轉(zhuǎn)換標準格式的示例代碼
更新時間:2023年05月23日 09:37:29 作者:ruantianqing
這篇文章主要介紹了JS時間轉(zhuǎn)換標準格式、時間戳轉(zhuǎn)換標準格式的示例代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
JS時間轉(zhuǎn)換標準格式、時間戳轉(zhuǎn)換標準格式
一、當前時間轉(zhuǎn)換為標準格式:
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二、時間戳轉(zhuǎn)換為標準格式:
function formatDate(datetime) {
var date = new Date(datetime); //datetime時間戳為13位毫秒級別,如為10位需乘1000
var month = ("0" + (date.getMonth() + 1)).slice(-2), // getMonth是從1-11,所以需要加1
sdate = ("0" + date.getDate()).slice(-2), // -2表示從倒數(shù)第二個元素開始截取
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 thatDate;
}
console.log(formatDate(1599085447000)) //2020-09-03 06:24:07js時間戳轉(zhuǎn)換2023-03-03 11:11:11格式方法
export function toTime(timestamp) {
//時間戳轉(zhuǎn)換方法
if (timestamp ?? '' != '') {
const date = new Date(timestamp);
const formattedDate = date.getFullYear() + '-' + ('0' + (date.getMonth() + 1)).slice(-2) + '-' + ('0' + date
.getDate()).slice(-2); // 格式化日期
const formattedTime = ('0' + date.getHours()).slice(-2) + ':' + ('0' + date.getMinutes()).slice(-2) + ':' + (
'0' +
date.getSeconds()).slice(-2); // 格式化時間
return formattedDate + ' ' + formattedTime; // 拼接日期和時間
} else {
return ''
}
}到此這篇關于JS時間轉(zhuǎn)換標準格式、時間戳轉(zhuǎn)換標準格式的示例代碼的文章就介紹到這了,更多相關js時間戳轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關文章
微信小程序?qū)崿F(xiàn)通過雙向滑動縮放圖片大小的方法
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)通過雙向滑動縮放圖片大小的方法,結(jié)合實例形式分析了微信小程序事件響應及圖片元素屬性動態(tài)操作相關實現(xiàn)技巧,需要的朋友可以參考下2018-12-12
微信小程序中跨頁面調(diào)用函數(shù)來刷新頁面的方法
在微信小程序中,不同頁面之間的通信和狀態(tài)管理是常見需求,比如:詳情頁數(shù)據(jù)更新,要刷新列表頁數(shù)據(jù),下面介紹幾種在Page中調(diào)用另一個Page函數(shù)或刷新頁面的方法,感興趣的朋友一起看看吧2025-06-06

