2017-04-26 56 views
2

當我得到像2017-05-05T00:00:00May 15, 2017這樣的值時,此工作正常,但在傳遞類似new Date();之類的值時總是失敗。我如何獲得它也接受JavaScript日期對象?當傳遞javascript日期對象時,瞬間返回無效日期

moment.utc(dt, [moment.ISO_8601, 'MMMMDDY h:mm:ss A']).add(1, 'second'); 

編輯---

需要明確的是,這條線失敗,invalid datedt = new Date();

回答

1

您的方法失敗,因爲momentjs的utc()功能期待第一個參數是日期string和當您將第二個String數組作爲參數傳遞時,不是Date對象。以下是從矩允許的方法簽名Docs-

moment.utc(); 
moment.utc(Number); 
moment.utc(Number[]); 
moment.utc(String); 
moment.utc(String, String); 
moment.utc(String, String[]); 
moment.utc(String, String, String); 
moment.utc(Moment); 
moment.utc(Date); 

這裏是一個工作原型爲您的查詢 -

var current = new Date(); 
 

 
var testDate = moment.utc(current).add(1,'s').format("MMMM Do YYYY, h:mm:ss"); 
 

 
console.log(testDate);
<script src="https://momentjs.com/downloads/moment.min.js"></script>

+0

當我通過'DT =新的日期()'它以無效的日期失敗。我需要爲格式數組添加一個值嗎? – Legion

+0

你是否正確地將'dt'對象傳遞給了momentjs?你可以在你想要做什麼的地方發佈你的代碼嗎? –

+0

@Legion基於你的問題更新了上面的代碼。 –

相關問題