sourcecode

자바스크립트 날짜 + 7일

codebag 2023. 9. 25. 22:37
반응형

자바스크립트 날짜 + 7일

대본이 왜 이래요?

내 시계를 29/04/2011이라고 하면 주 입력에 36/4/2011이 추가됩니다. 하지만 정확한 날짜는 6/5/2011이어야 합니다.

var d = new Date();
var curr_date = d.getDate();
var tomo_date = d.getDate()+1;
var seven_date = d.getDate()+7;
var curr_month = d.getMonth();
curr_month++;
var curr_year = d.getFullYear();
var tomorrowsDate =(tomo_date + "/" + curr_month + "/" + curr_year);
var weekDate =(seven_date + "/" + curr_month + "/" + curr_year);
{
jQuery("input[id*='tomorrow']").val(tomorrowsDate);
jQuery("input[id*='week']").val(weekDate);
    }

var date = new Date();
date.setDate(date.getDate() + 7);

console.log(date);

그리고 네, 이것도 가능합니다.date.getDate() + 7월의 마지막 날보다 큽니다.자세한 내용은 MDN을 참조하십시오.

신고불포함

타임스탬프를 반환하려면

new Date().setDate(new Date().getDate() + 7)

반품일자

new Date(new Date().setDate(new Date().getDate() + 7))

이런 거?

var days = 7;
var date = new Date();
var res = date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
alert(res);

다시 날짜로 변환:

date = new Date(res);
alert(date)

또는 다음을 선택할 수 있습니다.

date = new Date(res);

// hours part from the timestamp
var hours = date.getHours();

// minutes part from the timestamp
var minutes = date.getMinutes();

// seconds part from the timestamp
var seconds = date.getSeconds();

// will display time in 10:30:23 format
var formattedTime = date + '-' + hours + ':' + minutes + ':' + seconds;
alert(formattedTime)

한 줄로:

new Date(Date.now() + 7 * 24 * 60 * 60 * 1000)

날짜 x일을 미래에 얻을 수 있는 간단한 방법은 날짜를 늘리는 것입니다.

function addDays(dateObj, numDays) {
  return dateObj.setDate(dateObj.getDate() + numDays);
}

예를 들어 제공된 날짜 개체를 수정할 수 있습니다.

function addDays(dateObj, numDays) {
   dateObj.setDate(dateObj.getDate() + numDays);
   return dateObj;
}

var now = new Date();
var tomorrow = addDays(new Date(), 1);
var nextWeek = addDays(new Date(), 7);

alert(
    'Today: ' + now +
    '\nTomorrow: ' + tomorrow +
    '\nNext week: ' + nextWeek
);

날짜 개체의 메소드를 사용하면 도움이 될 입니다.

예:

myDate = new Date();
plusSeven = new Date(myDate.setDate(myDate.getDate() + 7));
var days = 7;
var date = new Date();
var res = date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));

var d = new Date(res);
var month = d.getMonth() + 1;
var day = d.getDate();

var output = d.getFullYear() + '/' +
    (month < 10 ? '0' : '') + month + '/' +
    (day < 10 ? '0' : '') + day;

$('#txtEndDate').val(output);

var future = new Date(); // get today date
future.setDate(future.getDate() + 7); // add 7 days
var finalDate = future.getFullYear() +'-'+ ((future.getMonth() + 1) < 10 ? '0' : '') + (future.getMonth() + 1) +'-'+ future.getDate();
console.log(finalDate);

다음 예제에서 요일을 추가하거나 늘리면 도움이 될 것입니다.어디 보자..

        // Current date
        var currentDate = new Date();
        // to set Bangladeshi date need to add hour 6           

        currentDate.setUTCHours(6);            
        // here 2 is day increment for the date and you can use -2 for decrement day
        currentDate.setDate(currentDate.getDate() + parseInt(2));

        // formatting date by mm/dd/yyyy
        var dateInmmddyyyy = currentDate.getMonth() + 1 + '/' + currentDate.getDate() + '/' + currentDate.getFullYear();           

여기에 두 가지 문제가 있습니다.

  1. seven_date날짜가 아니라 숫자입니다.29 + 7 = 36
  2. getMonth0을 기준으로 해당 월의 인덱스를 반환합니다.따라서 하나를 추가하면 현재 월 번호를 얻을 수 있습니다.

언급URL : https://stackoverflow.com/questions/5741632/javascript-date-7-days

반응형