JavaScript วิธีรับวันพรุ่งนี้ในรูปแบบ dd-mm-yy


91

ฉันพยายามให้ JavaScript แสดงวันที่พรุ่งนี้ในรูปแบบ (dd-mm-yyyy)

ฉันได้รับสคริปต์นี้ซึ่งแสดงวันที่ของวันนี้ในรูปแบบ (dd-mm-yyyy)

var currentDate = new Date()
var day = currentDate.getDate()
var month = currentDate.getMonth() + 1
var year = currentDate.getFullYear()
document.write("<b>" + day + "/" + month + "/" + year + "</b>")

Displays: 25/2/2012 (todays date of this post)

แต่จะให้แสดงวันพรุ่งนี้ในรูปแบบเดียวกันได้อย่างไร 26/2/2012

ฉันลองสิ่งนี้:

var day = currentDate.getDate() + 1

อย่างไรก็ตามฉันสามารถรักษา+1และไปได้มากกว่า 31 วันเห็นได้ชัดว่าไม่มี> 32 วันในหนึ่งเดือน

ค้นหามาหลายชั่วโมงแล้ว แต่ดูเหมือนจะไม่มีคำตอบหรือวิธีแก้ปัญหานี้ใช่หรือไม่?

คำตอบ:


178

สิ่งนี้ควรแก้ไขได้ดีจริงสำหรับคุณ

หากคุณผ่านตัวสร้างวันที่สักครั้งมันจะทำงานที่เหลือ

24 ชั่วโมง 60 นาที 60 วินาที 1,000 มิลลิวินาที

var currentDate = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
var day = currentDate.getDate()
var month = currentDate.getMonth() + 1
var year = currentDate.getFullYear()
document.write("<b>" + day + "/" + month + "/" + year + "</b>")

สิ่งหนึ่งที่ควรทราบก็คือวิธีนี้จะส่งกลับวันที่ 24 ชั่วโมงนับจากนี้ซึ่งอาจคลาดเคลื่อนได้ในช่วงเวลาออมแสง

คำตอบของ Phil ทำงานได้ตลอดเวลา:

var currentDate = new Date();
currentDate.setDate(currentDate.getDate() + 1);

เหตุผลที่ฉันแก้ไขโพสต์ของฉันเป็นเพราะฉันเองสร้างจุดบกพร่องที่เกิดขึ้นในช่วง DST โดยใช้วิธีการเดิมของฉัน


9
ขอบคุณสั้นกว่านี้หน่อยvar currentDate = new Date(+new Date() + 86400000);
Ikrom

13
โปรดทราบว่าด้วยกลยุทธ์นี้คุณอาจพบปัญหาเกี่ยวกับ DST ซึ่งทำให้วันหนึ่งของปีมี 23 ชั่วโมงและอีกวันหนึ่งมี 25 ชั่วโมงคำตอบของ Phil ด้านล่างหลีกเลี่ยงปัญหานี้
gsf

3
new Date().getTime()สามารถประยุกต์เป็นDate.now()
Czerny

ถูกต้องทั้งคู่ฉันไม่คิดว่า IE8 มี Date.now ()
Roderick Obrist

2
@ConorB, .getMonth () ส่งคืน 0 สำหรับมกราคม 1 สำหรับกุมภาพันธ์ ... ฯลฯ 11 สำหรับธันวาคม การเพิ่ม 1 จะเปลี่ยนจากดัชนีอาร์เรย์เป็นวันที่ที่มนุษย์อ่านได้
Roderick Obrist

136

DateคลาสJavaScript จะจัดการสิ่งนี้ให้คุณ

var d = new Date("2012-02-29")
console.log(d)
// Wed Feb 29 2012 11:00:00 GMT+1100 (EST)

d.setDate(d.getDate() + 1)
console.log(d)
// Thu Mar 01 2012 11:00:00 GMT+1100 (EST)

console.log(d.getDate())
// 1

จะยังใช้ได้ไหมถ้าวันนี้เป็นวันสุดท้ายของเดือนเช่นวันที่ 31 ถ้าคุณเพิ่ม +1 คุณจะไม่ลงเอยด้วย 32nd?
Timo

19
@Timo ค่อนข้างแน่ใจว่าตัวอย่างของฉันแสดงให้เห็นอย่างแน่นอน
ฟิล

1
โอ้ใช่คุณพูดถูก ฉันลืมไปว่ากุมภาพันธ์มีเพียง 28 วันเท่านั้น :-)
Timo

สิ่งนี้ล้มเหลวสำหรับฉันในวันนี้เนื่องจากวันที่ใหม่ ('2016-10-31') ส่งคืน 'อาทิตย์ 30 ตุลาคม 2559 23:00:00 GMT-0100 (AZOT)' ฉันอยู่ในหมู่เกาะอะซอเรสและเปลี่ยนเขตเวลาจาก AZOST เป็น AZOT
nunoarruda

@nunoarruda ขออภัยไม่แน่ใจจริงๆว่าคุณพูดอะไรที่นั่นหรือเกี่ยวข้องกับคำตอบนี้
ฟิล

7

ฉันจะใช้ไลบรารี DateJS มันสามารถทำได้อย่างแน่นอน

http://www.datejs.com/

ดำเนินการดังต่อไปนี้:

var d = new Date.today().addDays(1).toString("dd-mm-yyyy");

Date.today() - ให้วันนี้ตอนเที่ยงคืน


1
ฉันชอบคำตอบของ Phil มากกว่า ... ฉันใช้ DateJS สำหรับทุกวัน - แต่ดูเหมือนว่าจะทำได้โดยใช้ JS เพียงอย่างเดียว!
MattW

4
Date.parse ('พรุ่งนี้') toString ('dd-MM-yyyy');
geoffrey.mcgill

5

Method Date.prototype.setDate ()ยอมรับแม้แต่อาร์กิวเมนต์ที่อยู่นอกช่วงมาตรฐานและเปลี่ยนวันที่ให้สอดคล้องกัน

function getTomorrow() {
    const tomorrow = new Date();
    tomorrow.setDate(tomorrow.getDate() + 1); // even 32 is acceptable
    return `${tomorrow.getFullYear()}/${tomorrow.getMonth() + 1}/${tomorrow.getDate()}`;
}

4

ด้านล่างนี้ใช้การรวมกันของคำตอบของ Roderick และ Phil โดยมีเงื่อนไขพิเศษสองข้อที่คิดเป็นตัวเลขหลักเดือน / วัน

API จำนวนมากที่ฉันทำงานด้วยมีความพิถีพิถันเกี่ยวกับเรื่องนี้และกำหนดให้วันที่มีตัวเลขแปดหลัก (เช่น '02022017') แทนที่จะเป็นตัวเลข 6 หรือ 7 หลักที่คลาสวันที่จะให้คุณในบางสถานการณ์

function nextDayDate() {
      // get today's date then add one
      var nextDay = new Date();
      nextDay.setDate(nextDay.getDate() + 1);

      var month = nextDay.getMonth() + 1;
      var day = nextDay.getDate();
      var year = nextDay.getFullYear();

      if (month < 10) { month = "0" + month } 
      if (day < 10) { day = "0" + day }

      return month + day + year;
}

3

ใช้กรณี:

Date.tomorrow() // 1 day next 
Date.daysNext(1) // alternative Date.tomorrow()
Date.daysNext(2) // 2 days next. 

ถ้า "พรุ่งนี้" ไม่ได้ขึ้นอยู่กับวันนี้ แต่เป็นวันอื่นที่แตกต่างจาก Date.now()อย่าใช้วิธีการคงที่ แต่คุณต้องใช้แบบไม่คงที่:

คือศ. 05 ธ.ค. 2551

 var dec5_2008=new Date(Date.parse('2008/12/05'));
 dec5_2008.tomorrow(); // 2008/12/06
    dec5_2008.tomorrow().day // 6
    dec5_2008.tomorrow().month // 12
    dec5_2008.tomorrow().year //2008
 dec5_2008.daysNext(1); // the same as previous
 dec5_2008.daysNext(7) // next week :)

API:

Dateold=Date;function Date(e){var t=null;if(e){t=new Dateold(e)}else{t=new Dateold}t.day=t.getDate();t.month=t.getMonth()+1;t.year=t.getFullYear();return t}Date.prototype.daysNext=function(e){if(!e){e=0}return new Date(this.getTime()+24*60*60*1e3*e)};Date.prototype.daysAgo=function(e){if(!e){e=0}return Date.daysNext(-1*e)};Date.prototype.tomorrow=function(){return this.daysNext(1)};Date.prototype.yesterday=function(){return this.daysAgo(1)};Date.tomorrow=function(){return Date.daysNext(1)};Date.yesterday=function(){return Date.daysAgo(1)};Date.daysNext=function(e){if(!e){e=0}return new Date((new Date).getTime()+24*60*60*1e3*e)};Date.daysAgo=function(e){if(!e){e=0}return Date.daysNext(-1*e)}

3

วิธีที่ 1: หากคุณไม่มีปัญหาในการใช้ไลบรารีอื่นสิ่งนี้สามารถใช้ได้กับคุณโดยใช้moment.js

moment().add('days', 1).format('L');

วิธีที่ 2: ใช้ Date.js

<script type="text/javascript" src="date.js"></script>    
var tomorrow = new Date.today().addDays(1).toString("dd-mm-yyyy"); 

วิธีนี้ใช้ไลบรารีภายนอกไม่ใช่ไลบรารี Date ดั้งเดิม เนื่องจาก bootstrap-datetimepicker ของฉันใช้ moment.js และไลบรารีวันที่ดั้งเดิมฉันจึงชอบวิธีที่ 1 คำถามนี้กล่าวถึงวิธีการเหล่านี้และวิธีการอื่น ๆ


2

มันง่ายมาก:

1: สร้างวัตถุวันที่ด้วยวันที่และเวลาของวันนี้ 2: ใช้เมธอดอ็อบเจ็กต์วันที่เพื่อดึงข้อมูลวันเดือนและปีเต็มและเชื่อมต่อเข้าด้วยกันโดยใช้ตัวดำเนินการ +

ไปที่http://www.thesstech.com/javascript/date-time JavaScript เพื่อดูบทแนะนำโดยละเอียดเกี่ยวกับวันที่และเวลา

รหัสตัวอย่าง:

  var my_date = new Date();  
  var tomorrow_date =       (my_date .getDate()+1)  + "-" + (my_date .getMonth()+1) + "-" + my_date .getFullYear();
  document.write(tomorrow_date);

สิ่งนี้ไม่ได้หมุนเวียนแบบวันต่อเดือนตามที่ OP สังเกตแล้ว
Wolfgang Kuehn

1

ใช้ JS เท่านั้น (Pure js)

วันนี้

new Date()
//Tue Oct 06 2020 12:34:29 GMT+0530 (India Standard Time)
new Date(new Date().setHours(0, 0, 0, 0))
//Tue Oct 06 2020 00:00:00 GMT+0530 (India Standard Time)
new Date(new Date().setHours(0, 0, 0,0)).toLocaleDateString('fr-CA')
//"2020-10-06"

พรุ่งนี้

new Date(+new Date() + 86400000);
//Wed Oct 07 2020 12:44:02 GMT+0530 (India Standard Time)
new Date(+new Date().setHours(0, 0, 0, 0) + 86400000);
//Wed Oct 07 2020 00:00:00 GMT+0530 (India Standard Time)
new Date(+new Date().setHours(0, 0, 0,0)+ 86400000).toLocaleDateString('fr-CA')
//"2020-10-07"
//don't forget the '+' before new Date()

วันมะรืนนี้

เพียงแค่คูณด้วยสองตัวอย่าง: - 2 * 86400000

คุณสามารถค้นหารหัสย่อของภาษาทั้งหมดได้จากhttps://stackoverflow.com/a/3191729/7877099


0
function getMonday(d)
{
   // var day = d.getDay();
   var day = @Config.WeekStartOn
   diff = d.getDate() - day + (day == 0 ? -6 : 0);
   return new Date(d.setDate(diff));
}

0

เหมือนกับคำตอบเดิม แต่อยู่ในบรรทัดเดียว:

var tomorrow = new Date(Date.now() + 24 * 60 * 60 * 1000)

ตัวเลขยืนเป็นเวลา 24 ชั่วโมง 60 นาที 60 วินาที 1,000 มิลลิวินาที


1
คำตอบนี้ไม่ถูกต้องเนื่องจากไม่ได้นำ DST มาพิจารณา
ผิดหวัง

0

คุณสามารถลองสิ่งนี้:

function Tomorrow(date=false) {
    var givendate = (date!=false) ? new Date(date) : new Date();
    givendate.setDate(givendate.getDate() + 1);
    var day = givendate.getUTCDate()
    var month = givendate.getUTCMonth()+1
    var year = givendate.getUTCFullYear()
    result ="<b>" + day + "/" + month + "/" + year + "</b>";
    return result;
} 
var day = Tomorrow('2020-06-30');
console.log('tomorrows1: '+Tomorrow('2020-06-30'));
console.log('tomorrows2: '+Tomorrow());



-1
        //-----------Date Configuration march 18,2014----------------------

        //alert(DateFilter);

        var date = new Date();
        y = date.getFullYear(), m = date.getMonth();
        var EndDate = new Date();



        switch (DateFilter) {
            case 'today': var StartDate = EndDate;   //todays date                 
                break;
            case 'yesterday':
                var d = new Date();
                var previousDate = new Date(d.getTime() - 1000 * 60 * 60 * 24);
                var StartDate = new Date(previousDate.yyyymmdd()); //yesterday Date
                break;
            case 'tomorrow':
                var d = new Date();
                var NextDate = new Date(d.getTime() + 1000 * 60 * 60 * 24);
                var StartDate = new Date(NextDate.yyyymmdd()); //tomorrow Date
                break;
            case 'thisweek': var StartDate = getMonday(new Date()); //1st date of this week
                break;
            case 'thismonth': var StartDate = new Date(y, m, 1);  //1st date of this month
                break;
            case 'thisyear': var StartDate = new Date("01/01/" + date.getFullYear());  //1st date of this year
                break;
            case 'custom': //var StartDate = $("#txtFromDate").val();                   
                break;
            default:
                var d = new Date();
                var StartDate = new Date(d.getTime() - 30 * 24 * 60 * 60 * 1000); //one month ago date from now.
        }


        if (DateFilter != "custom") {
            var SDate = $.datepicker.formatDate('@Config.DateFormat', StartDate); $("#txtFromDate").val(SDate);
            var EDate = $.datepicker.formatDate('@Config.DateFormat', EndDate); $("#txtToDate").val(EDate);
        }
        //-----------Date Configuration march 18,2014----------------------

โปรดพิจารณาเพิ่มคำอธิบายในคำตอบของคุณ
Amar

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.