简体   繁体   English

如何将 1 天添加到当前日期?

[英]How can I add 1 day to current date?

I have a current Date object that needs to be incremented by one day using the JavaScript Date object. I have the following code in place:我有一个当前日期 object,需要使用 JavaScript 日期 object 增加一天。我有以下代码:

var ds = stringFormat("{day} {date} {month} {year}", { 
    day: companyname.i18n.translate("day", language)[date.getUTCDay()], 
    date: date.getUTCDate(), 
    month: companyname.i18n.translate("month", language)[date.getUTCMonth()], 
    year: date.getUTCFullYear() 
});

How can I add one day to it?我怎样才能增加一天呢?

I've added +1 to getUTCDay() and getUTCDate() but it doesn't display 'Sunday' for day , which I am expecting to happen.我已将 +1 添加到getUTCDay()getUTCDate()但它不会显示day的“星期日”,这是我期望发生的。

To add one day to a date object:将一天添加到日期对象:

var date = new Date();

// add a day
date.setDate(date.getDate() + 1);

In my humble opinion the best way is to just add a full day in milliseconds, depending on how you factor your code it can mess up if you are on the last day of the month.在我看来,最好的方法是以毫秒为单位添加一整天,这取决于您如何考虑代码,如果您在本月的最后一天,它可能会搞砸。

For example Feb 28 or march 31.例如 2 月 28 日或 3 月 31 日。

Here is an example of how I would do it:这是我将如何做的一个例子:

var current = new Date(); //'Mar 11 2015' current.getTime() = 1426060964567
var followingDay = new Date(current.getTime() + 86400000); // + 1 day in ms
followingDay.toLocaleDateString();

Imho this insures accuracy恕我直言,这确保了准确性

Here is another example.这是另一个例子。 I do not like that.我不喜欢那个。 It can work for you but not as clean as example above.它可以为您工作,但不如上面的示例那么干净。

var today = new Date('12/31/2015');
var tomorrow = new Date(today);
tomorrow.setDate(today.getDate()+1);
tomorrow.toLocaleDateString();

Imho this === 'POOP'恕我直言this === 'POOP'

So some of you have had gripes about my millisecond approach because of day light savings time.因此,由于夏令时,你们中的一些人对我的毫秒方法感到不满。 So I'm going to bash this out.所以我要抨击这一点。 First, Some countries and states do not have Day light savings time.首先,一些国家和州没有夏令时。 Second Adding exactly 24 hours is a full day.第二 正好加上 24 小时是一整天。 If the date number does not change once a year but then gets fixed 6 months later I don't see a problem there.如果日期编号每年不更改一次,但在 6 个月后得到修复,我认为那里没有问题。 But for the purpose of being definite and having to deal with allot the evil Date() I have thought this through and now thoroughly hate Date.但为了明确并不得不处理分配邪恶的Date() ,我已经考虑过这一点,现在彻底讨厌 Date。 So this is my new Approach.所以这是我的新方法。

var dd = new Date(); // or any date and time you care about 
var dateArray =  dd.toISOString().split('T')[0].split('-').concat( dd.toISOString().split('T')[1].split(':') );
// ["2016", "07", "04", "00", "17", "58.849Z"] at Z 

Now for the fun part!现在是有趣的部分!

var date = { 
    day: dateArray[2],
    month: dateArray[1],
    year: dateArray[0],
    hour: dateArray[3],
    minutes: dateArray[4],
    seconds:dateArray[5].split('.')[0],
    milliseconds: dateArray[5].split('.')[1].replace('Z','')
}

Now we have our Official Valid international Date Object clearly written out at Zulu meridian.现在我们在祖鲁子午线清楚地写出了我们的官方有效国际日期对象。 Now to change the date现在更改日期

dd.setDate(dd.getDate()+1); // this gives you one full calendar date forward
tomorrow.setDate(dd.getTime() + 86400000);// this gives your 24 hours into the future. do what you want with it.

如果您想在当前日期时间中添加一天(24 小时),您可以像这样添加毫秒:

new Date(Date.now() + ( 3600 * 1000 * 24))
int days = 1;
var newDate = new Date(Date.now() + days*24*60*60*1000);

CodePen代码笔

 var days = 2; var newDate = new Date(Date.now()+days*24*60*60*1000); document.write('Today: <em>'); document.write(new Date()); document.write('</em><br/> New: <strong>'); document.write(newDate);

Inspired by jpmottin in this question , here's the one line code:jpmottin in this question的启发,这是一行代码:

 var dateStr = '2019-01-01'; var days = 1; var result = new Date(new Date(dateStr).setDate(new Date(dateStr).getDate() + days)); document.write('Date: ', result); // Wed Jan 02 2019 09:00:00 GMT+0900 (Japan Standard Time) document.write('<br />'); document.write('Trimmed Date: ', result.toISOString().substr(0, 10)); // 2019-01-02

Hope this helps希望这可以帮助

simply you can do this只是你可以做到这一点

 var date = new Date(); date.setDate(date.getDate() + 1); console.log(date);

now the date will be the date of tomorrow.现在date将是明天的日期。 here you can add or deduct the number of days as you wish.在这里您可以根据需要添加或减少天数。

This is function you can use to add a given day to a current date in javascript.这是您可以用来在 javascript 中将给定日期添加到当前日期的函数。

function addDayToCurrentDate(days){
  let currentDate = new Date()
  return new Date(currentDate.setDate(currentDate.getDate() + days))
}

// current date = Sun Oct 02 2021 13:07:46 GMT+0200 (South Africa Standard Time)
// days = 2

console.log(addDayToCurrentDate(2))
// Mon Oct 04 2021 13:08:18 GMT+0200 (South Africa Standard Time)

 // Function gets date and count days to add to passed date function addDays(dateTime, count_days = 0){ return new Date(new Date(dateTime).setDate(dateTime.getDate() + count_days)); } // Create some date const today = new Date("2022-02-19T00:00:00Z"); // Add some days to date const tomorrow = addDays(today, 1); // Result console.log("Tomorrow => ", new Date(tomorrow).toISOString()); // 2022-02-20T00:00:00.000Z

We can get date of the day after today by using timedelta with numOfDays specified as 1 below.我们可以通过将 numOfDays 指定为 1 的 timedelta 来获取今天后一天的日期。

from datetime import date, timedelta

tomorrow = date.today() + timedelta(days=1)
currentDay = '2019-12-06';
currentDay = new Date(currentDay).add(Date.DAY, +1).format('Y-m-d');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM