js模仿php中strtotime()與date()函數(shù)實(shí)現(xiàn)方法
本文實(shí)例講述了js模仿php中strtotime()與date()函數(shù)實(shí)現(xiàn)方法。分享給大家供大家參考。具體如下:
在js中沒有像php中strtotime()與date()函數(shù),可直接轉(zhuǎn)換時(shí)間戳,下面我們來自定一個(gè)函數(shù)來實(shí)現(xiàn)js中具體有時(shí)間戳轉(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)換后的時(shí)間戳為: '+unix+' '); var unix = 1353033300; var datetime = unix_to_datetime(unix); document.write(unix+' 轉(zhuǎn)換后的日期為: '+datetime);
如果想彈出:2010-10-20 10:00:00這個(gè)格式的也好辦
<script> function getLocalTime(nS) { return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " "); } alert(getLocalTime(1177824835)); </script>
完整實(shí)例
<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è)計(jì)有所幫助。
相關(guān)文章
如何使用js正則表達(dá)式驗(yàn)證文件夾名是否符合規(guī)范
眾所周知正則表達(dá)式非常強(qiáng)大,下面這篇文章主要給大家介紹了關(guān)于如何使用js正則表達(dá)式驗(yàn)證文件夾名是否符合規(guī)范的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05js實(shí)現(xiàn)精確到秒的倒計(jì)時(shí)效果
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)精確到秒的倒計(jì)時(shí)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05怎樣使用?JavaScript?轉(zhuǎn)義字符串中的引號
要轉(zhuǎn)義字符串中的單引號或雙引號,需要在字符串內(nèi)容中的每個(gè)單引號或雙引號之前使用反斜杠?\?字符,例如?‘that’s?it’,這篇文章主要介紹了如何使用?JavaScript?轉(zhuǎn)義字符串中的引號,需要的朋友可以參考下2023-11-11Layui Table js 模擬選中checkbox的例子
今天小編就為大家分享一篇Layui Table js 模擬選中checkbox的例子,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09JavaScript中常用的3種彈出提示框(alert、confirm、prompt)
這篇文章主要給大家介紹了關(guān)于JavaScript中常用的3種彈出提示框(alert、confirm、prompt)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11