Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get product given date

Tags:

javascript

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);
like image 941
Ailyn Avatar asked Dec 19 '25 16:12

Ailyn


1 Answers

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

like image 105
Kishor V Avatar answered Dec 22 '25 07:12

Kishor V



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!