2012-03-26 80 views
87

我想將日期轉換爲時間戳,我的輸入是26-02-2012。我用將日期轉換爲JavaScript中的時間戳?

new Date(myDate).getTime(); 

它說NaN ..任何一個可以告訴如何轉換這?

+0

可能的重複http://stackoverflow.com/questions/9343971/timestamp-conversion-javascript – 2012-03-26 13:40:45

+0

你可能想看看date.js庫:http://www.datejs.com/ – rsbarro 2012-03-26 13:41:05

+1

你有沒有使用Date(myDate).getTime()(你標記爲代碼),還是在它意味着代碼的一部分之前是「new」這個詞?你付出的努力越多,你得到的答案就越好。 – 2012-03-26 13:41:13

回答

117
var myDate="26-02-2012"; 
myDate=myDate.split("-"); 
var newDate=myDate[1]+","+myDate[0]+","+myDate[2]; 
alert(new Date(newDate).getTime());​ //will alert 1330192800000 

更新:

var myDate="26-02-2012"; 
myDate=myDate.split("-"); 
var newDate=myDate[1]+"/"+myDate[0]+"/"+myDate[2]; 
alert(new Date(newDate).getTime()); //will alert 1330210800000 

DEMO(在Chrome,FF,歌劇,IE和Safari測試)。

+0

不幸的是,這在Safari5中不起作用,因爲它返回'NaN'。在Safari中,您必須使用其他可能的構造函數'new Date(year,month,day);',關於這個例子:'new Date(myDate [2],myDate [1],myDate [0]);' – insertusernamehere 2012-08-20 11:18:43

+4

Check更新。 – 2012-08-20 12:14:14

+6

而不是將日期字符串從「歐洲」轉換爲「美國」格式,最好將其轉換爲ISO 8601格式('YYYY-MM-DD'),並保證可以通過Date()一般來說,是日期字符串最具互操作性的格式。 – 2014-12-04 13:54:03

2

您的字符串未採用Date對象爲specified to handle的格式。您必須自己解析它,使用日期解析庫(如MomentJS)或更舊的(並且目前尚未維護的,據我所知)DateJS,或在請求Date之前將其按照正確的格式(例如,2012-02-29)解析它。

爲什麼你要NaN:當你問new Date(...)處理一個無效的字符串,它返回被設置爲無效日期(new Date("29-02-2012").toString()回報"Invalid date")一個Date對象。在此狀態下的日期對象上調用getTime()將返回NaN

+0

UTC的另一個:http://arshaw.com/xdate/ – benvds 2012-03-26 13:53:41

+0

@benvds:很酷,謝謝。雖然我發現註釋*「另外,它對DOM沒有破壞性」*有點奇怪......我期望他們的意思是它不會改變'Date'對象(它與DOM)。 – 2012-03-26 14:06:20

3

你只需要扭轉你的日期數字,改變-,

new Date(2012,01,26).getTime(); // 02 becomes 01 because getMonth() method returns the month (from 0 to 11) 

你的情況:

var myDate="26-02-2012"; 
myDate=myDate.split("-"); 
new Date(parseInt(myDate[2], 10), parseInt(myDate[1], 10) - 1 , parseInt(myDate[0]), 10).getTime(); 

附:英國的語言環境在這裏並不重要。

+0

該日期格式也無效,並且無法可靠地跨瀏覽器和跨語言環境(例如,對於我在Chrome中使用英國語言​​環境的用戶)。如果你打算建議一種格式,建議一個實際記錄工作的格式。 – 2012-03-26 13:47:55

+0

我從https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date得到了這個例子。我忘了放下繩子。現在它可以工作。 – antonjs 2012-03-26 13:52:37

+2

好吧,至少現在上面的代碼沒有使用無效的日期格式 - 它只是給出了錯誤的日期,原因有兩個。上面你已經定義了2014年3月2日的日期(你現場訂單搞砸了)。如果這些字段按正確的順序排列,則可以定義日期** 2012年3月26日(月份值從零開始)。但是,由於OP有一串字符串,而不是一系列數字,即使你解決了這些問題,它也不是那麼有用。 – 2012-03-26 14:04:03

20
var dtstr = "26-02-2012"; 
new Date(dtstr.split("-").reverse().join("-")).getTime(); 
4
function getTimeStamp() { 
     var now = new Date(); 
     return ((now.getMonth() + 1) + '/' + (now.getDate()) + '/' + now.getFullYear() + " " + now.getHours() + ':' 
        + ((now.getMinutes() < 10) ? ("0" + now.getMinutes()) : (now.getMinutes())) + ':' + ((now.getSeconds() < 10) ? ("0" + now 
        .getSeconds()) : (now.getSeconds()))); 
} 
+0

冗長但很好的工作答案 – 2017-09-19 13:25:50

20

試試這個功能,它採用了Date.parse()方法,也不需要任何自定義邏輯:

function toTimestamp(strDate){ 
    var datum = Date.parse(strDate); 
    return datum/1000; 
} 
alert(toTimestamp('02/13/2009 23:31:30')); 
+0

這是迄今爲止最好的答案,謝謝! – gyo 2017-08-20 12:19:28

2
/** 
* Date to timestamp 
* @param string template 
* @param string date 
* @return string 
* @example   datetotime("d-m-Y", "26-02-2012") return 1330207200000 
*/ 
function datetotime(template, date){ 
    date = date.split(template[1]); 
    template = template.split(template[1]); 
    date = date[ template.indexOf('m') ] 
     + "/" + date[ template.indexOf('d') ] 
     + "/" + date[ template.indexOf('Y') ]; 

    return (new Date(date).getTime()); 
} 
+0

不錯的作品,@Evgeniy!感謝分享! – Ricalsin 2017-05-22 19:58:47

4

這個重構的代碼會做

let toTimestamp = strDate => Date.parse(strDate) 

這適用於除ie8之外的所有現代瀏覽器 -

+0

這是真正的答案。其餘的都是一個笑話。 – Moss 2018-01-17 13:23:08