2016-08-24 66 views
0

是否有簡單的方法將格式如下所示的日期轉換爲日期:2016-08-24 15:23:31.949284+03將以毫秒爲單位的unix時間戳轉換爲postgres可讀的時間戳

我需要這個,因爲我在客戶端js應用程序中有unix時間戳,而且我需要根據timestamptz來實現PostgreSQL數據庫的分頁。

我想這是基於對這個問題做分頁很好的解決方案:Postgres: using timestamps for pagination

+0

沒有在這裏? https://www.google.com/search?q=timestamptz+convert+epoch – mplungjan

+0

我發現的所有解決方案都忽略了unix時間戳中的毫秒數,但這對我來說至關重要 – stkvtflw

+0

我不認爲你有949284毫秒在您的時間戳 – mplungjan

回答

0

這適用於我的Chrome在荷蘭。不知道了toString()做同樣的話

function pad(num) { 
 
    return String("0"+num).slice(-2); 
 
} 
 
function toPost(ts) { 
 
    var d = new Date(ts); 
 
    var time = d.toString().split(d.getFullYear()+" ")[1].split("(")[0].slice(0,-3) 
 
    time = time.replace(/ GMT/,"."+d.getMilliseconds()) 
 
    return d.getFullYear()+"-"+pad(d.getMonth()+1)+"-"+pad(d.getDate())+" "+time; 
 
} 
 
alert(toPost(new Date().getTime())+"\n"+toPost(1471866155422)+"\n"+"2016-08-24 15:23:31.949284+03");

相關問題