[英]how to add an extra day to the iso format date in javascript
當前日期格式:2016-05-10T06:34:17Z,我需要在當前日期加上 10 天,即 ..2016-05-20T06:34:17 在 javascript 或 angularjs 中。
您可以使用Date
構造函數根據您的字符串創建一個新日期。
使用Date.prototype.setDate()
設置日期的日期。
例子:
var myDate = new Date('2016-05-10T06:34:17Z'); myDate.setDate(myDate.getDate() + parseInt(10)); console.log(myDate);
注意:如果您需要多次使用此腳本,您可以創建簡單的“實用程序”功能,例如:
var addDays = function(str, days) { var myDate = new Date(str); myDate.setDate(myDate.getDate() + parseInt(days)); return myDate; } var myDate = addDays('2016-05-10T06:34:17Z', 10); console.log(myDate);
相關文檔:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setDate
一班輪解決方案:
今天:
today = new Date().toISOString()
昨天:
yesterday = new Date(new Date().setDate(new Date().getDate() - 1)).toISOString()
明天:
tomorrow = new Date(new Date().setDate(new Date().getDate() + 1)).toISOString()
提示:您可以添加或減去天數以獲取從今天開始的確切日期。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.