js模仿php中strtotime()與date()函數(shù)實現(xiàn)方法
本文實例講述了js模仿php中strtotime()與date()函數(shù)實現(xiàn)方法。分享給大家供大家參考。具體如下:
在js中沒有像php中strtotime()與date()函數(shù),可直接轉(zhuǎn)換時間戳,下面我們來自定一個函數(shù)來實現(xiàn)js中具體有時間戳轉(zhuǎn)換的功能。
function datetime_to_unix(datetime){
var tmp_datetime = datetime.replace(/:/g,'-');
tmp_datetime = tmp_datetime.replace(/ /g,'-');
var arr = tmp_datetime.split("-");
var now = new Date(Date.UTC(arr[0],arr[1]-1,arr[2],arr[3]-8,arr[4],arr[5]));
return parseInt(now.getTime()/1000);
}
function unix_to_datetime(unix) {
var now = new Date(parseInt(unix) * 1000);
return now.toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");
}
var datetime = '2012-11-16 10:36:50';
var unix = datetime_to_unix(datetime);
document.write(datetime+' 轉(zhuǎn)換后的時間戳為: '+unix+'
');
var unix = 1353033300;
var datetime = unix_to_datetime(unix);
document.write(unix+' 轉(zhuǎn)換后的日期為: '+datetime);
如果想彈出:2010-10-20 10:00:00這個格式的也好辦
<script>
function getLocalTime(nS) {
return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");
}
alert(getLocalTime(1177824835));
</script>
完整實例
<script type="text/javascript">
var day1 = parseInt(new Date().valueOf()/1000);
var day2 = new Date(day1 * 1000);
function getLocalTime(nS) {
return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:d{1,2}$/,' ');
}
/* 同上面函數(shù) */
function getLocalTimes(nS) {
return new Date(parseInt(nS) * 1000).toLocaleString().substr(0,17);
}
function getLocalFormatTime(nS) {
return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");
}
document.getElementById("btn1").onclick = function(){
alert(day1);
}
document.getElementById("btn2").onclick = function(){
alert(day2.toLocaleString());
}
document.getElementById("btn3").onclick = function(){
alert( getLocalTime(day1) );
}
document.getElementById("btn4").onclick = function(){
alert( getLocalFormatTime(day1) );
}
document.getElementById("btn5").onclick = function(){
alert(day2.getFullYear()+"-"+(day2.getMonth()+1)+"-"+day2.getDate()+" "+day2.getHours()+":"+day2.getMinutes()+":"+day2.getSeconds());
}
</script>
希望本文所述對大家的javascript程序設(shè)計有所幫助。
相關(guān)文章
web-view內(nèi)嵌H5與uniapp數(shù)據(jù)的實時傳遞解決方案
這篇文章主要介紹了web-view內(nèi)嵌H5與uniapp數(shù)據(jù)的實時傳遞,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07
javascript實現(xiàn)鼠標(biāo)放上后下邊對應(yīng)內(nèi)容變換的效果
這篇文章主要介紹了javascript鼠標(biāo)放上后下邊對應(yīng)內(nèi)容變換的方法,實例分析了javascript實現(xiàn)tab切換的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08
javascript基本數(shù)據(jù)類型及類型檢測常用方法小結(jié)
這篇文章主要介紹了javascript基本數(shù)據(jù)類型及類型檢測常用方法,總結(jié)分析了javascript的基本數(shù)據(jù)類型與類型檢測的常用操作方法,具有一定參考借鑒價值,需要的朋友可以參考下2016-12-12
判斷用戶的在線狀態(tài) onbeforeunload事件
window.event.clientX和window.event.clientY 將捕捉當(dāng)前事件發(fā)生時鼠標(biāo)相對與窗口的桌面坐標(biāo),通常情況下IE的關(guān)閉按鈕都會在頁面的右上部分,所以點關(guān)閉的時候鼠標(biāo)的坐標(biāo)的Y坐標(biāo)一定是小于0的2011-03-03
前端Webpack配置之eval-source-map使用方法
eval-source-map 是 Webpack 中 devtool 選項的一種模式,它提供了一種內(nèi)聯(lián) Source Map 的方式,用于開發(fā)環(huán)境中的源代碼映射,這篇文章主要介紹了前端Webpack配置之eval-source-map使用方法,需要的朋友可以參考下2024-12-12

