3

How to properly get the date 3 days before the given, if I have a format like 07/21/2017 8:30 AM , this is because I use the format of Eonasdan DateTime Picker. But when I try to minus it with 3 days I got Fri Jul 21 2017 08:24:14 GMT+0800 (China Standard Time) as my value but my desired output should be the same format like 07/21/2017 8:30 AM. How can I get that the value even I follow my format?

My codes:

var d =new Date('07/21/2017 8:30 AM');
var yesterday = new Date(d.getTime() - (96*60*60));
alert(yesterday);
Ailyn
  • 265
  • 1
  • 6
  • 17
  • 6
    I strongly recommend taking a look at moment.js, it will make your life much easier when dealing with dates in JS. – Rob Jul 18 '17 at 08:10
  • 1
    Possible duplicate of [Subtract days from a date in JavaScript](https://stackoverflow.com/questions/1296358/subtract-days-from-a-date-in-javascript) – GajendraSinghParihar Jul 18 '17 at 08:17
  • I think my question is little bit the same but totally different, the question at https://stackoverflow.com/questions/1296358/subtract-days-from-a-date-in-javascript is just asking for subtracting current date into certain value without any format where if my question has a format to follow. Therefore, I can say my question is unique, not the same or no duplicate. @Champ – Ailyn Jul 18 '17 at 08:50

5 Answers5

1

This should do the trick:

var date = new Date('07/21/2017 8:30 AM'); // get
date.setDate(date.getDate() - 3);
console.log(date.toString());

Basically, it takes the current day with getDate() and uses setDate() to update the time to 3 days in the past.

If you want to format your date, look into moment.js like @Robert said. You could use something like:

moment().format('MM/DD/YYYY, h:mm:ss a');

More information on the Date class here and moment documentation here.

brennanenanen
  • 409
  • 5
  • 10
1

function formatDate(d) {
            month = '' + (d.getMonth() + 1),
            day = '' + d.getDate(),
            year = d.getFullYear();
    
        if (month.length < 2) month = '0' + month;
        if (day.length < 2) day = '0' + day;
    
        date = [month, day, year].join('/');
        var hours = d.getHours();
        var minutes = d.getMinutes();
        var ampm = hours >= 12 ? 'PM' : 'AM';
        hours = hours % 12;
        hours = hours ? hours : 12; // the hour '0' should be '12'
        minutes = minutes < 10 ? '0'+minutes : minutes;
        var strTime = hours + ':' + minutes + ' ' + ampm;
        return date+" "+strTime;
}
var d =new Date('07/21/2017 8:30 AM');
d.setDate(d.getDate() - 3);
var yesterday = formatDate(d);
alert(yesterday);

This should work

Kishor V
  • 431
  • 4
  • 11
0

Try using this

var d = new Date();
var d =new Date('07/21/2017 8:30 AM');
var yesterday = d;
yesterday.setDate(d.getDate()-3);
alert(yesterday);
0

You can simply subtract days from the Date E.g d.getDate() - 3

var d =new Date('07/21/2017 8:30 AM');
d.setDate(d.getDate() - 3);
alert(d)
GajendraSinghParihar
  • 9,051
  • 11
  • 36
  • 64
0

You need to use setDate function on Date class to change the date part of a date variable

    var d =new Date('07/21/2017 8:30 AM');
    d.setDate(d.getDate() - 3);
    console.debug(d);
GajendraSinghParihar
  • 9,051
  • 11
  • 36
  • 64
RBT
  • 24,161
  • 21
  • 159
  • 240
  • `setDate()` isn't an API, it's a built-in class method. – brennanenanen Jul 18 '17 at 08:22
  • @brennanenanen Does this sound correct - `setDate API on Date class`? I know calling it a method is certainly correct which you've advised. – RBT Jul 18 '17 at 08:23
  • API stands for application programming interface. API's are used to talk between applications and are not built-in classes of something like js. – brennanenanen Jul 18 '17 at 08:28
  • What you've said is certainly correct but that API is very applicable to public methods of a class as well. Please see [here](https://stackoverflow.com/a/7440416/465053). – RBT Jul 18 '17 at 08:36
  • Hmm, I guess. Stil, it isn't talking to an application and it's not using a programming interface. – brennanenanen Jul 18 '17 at 08:43