How to get yesterday date in node.js backend?
Asked Answered
E

8

12

I am using date-format package in node back end and I can get today date using

var today = dateFormat(new Date());

In the same or some other way I want yesterday date. Still I did't get any proper method. For the time being I am calculating yesterday date manually with lot of code. Is there any other method other then writing manually ?

Ephialtes answered 10/8, 2015 at 6:0 Comment(2)
possible duplicate of Subtract days from a date in JavaScriptAgnola
If we all could try to search first and post questions only if we don't find, that'd be great. And if we mark questions as duplicate instead of posting answers just to gain reps, that'd be great.Vicennial
M
32

Try this:

var d = new Date(); // Today!
d.setDate(d.getDate() - 1); // Yesterday!
Mucin answered 10/8, 2015 at 6:23 Comment(4)
what if the current date is 1st of January 2015? would yesterday still be equal to 31st December 2014 following this method?Paramatta
@Paramatta Yes, its.Mucin
I tried this. A -1 on 1st of March makes it 31st of Feb. Definitely incorrect.Daydream
I tried it with Node.js v12.21.0 and see the correct result: ``` Welcome to Node.js v12.21.0. Type ".help" for more information. > let d = new Date('2020-03-01') undefined > new Date(d.setDate(d.getDate() - 1)) 2020-02-28T00:00:00.000Z ```Leatherworker
M
7

I would take a look at moment.js if you are interested in doing calculations with dates, there are many issues you can run into trying to do it manually or even with the built in Date objects in JavaScript/node.js such as leap years and daylight savings time issues.

http://momentjs.com/

For example:

var moment = require('moment');
var yesterday = moment().subtract(1, 'days');
console.log(yesterday.format());
Mazel answered 10/8, 2015 at 6:26 Comment(0)
C
4

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

This will give you same Date format, but not tested for edge case scenarios.

Condom answered 24/7, 2020 at 17:8 Comment(0)
G
0

you can also change Hour,Minute,seconds and milliseconds attributes of time object like this.

var date = new Date();
    date.setDate(date.getDate()-1);
    date.setHours(hour);
    date.setMinutes(minute);
    date.setSeconds(seconds);
    date.setMilliseconds(milliseconds);
Guinn answered 10/8, 2015 at 7:17 Comment(1)
Please explain what your code does and why it will solve the problem. An answer that just contains code (even if it's working) usually wont help the OP to understand their problem. It's also recommended that you don't post an answer if it's just a guess. A good answer will have a plausible reason for why it could solve the OP's issue.Osset
C
0

To get the string in a format familiar to people

// Date String returned in format yyyy-mm-dd
function getYesterdayString(){
    var date = new Date();
    date.setDate(date.getDate() - 1);
    var day = ("0" + date.getDate()).slice(-2);
    var month = ("0" + (date.getMonth() + 1)).slice(-2); // fix 0 index
    return (date.getYear() + 1900) + '-' + month + '-' + day;
}
Calliopsis answered 24/6, 2018 at 7:32 Comment(0)
A
0

Try Library called node-datetime

var datetime = require('node-datetime');
var dt = datetime.create();
// 7 day in the past
dt.offsetInDays(-1);
var formatted = dt.format('Y-m-d H:M:S');
console.log(formatted)
Angy answered 29/11, 2018 at 12:28 Comment(0)
L
-1

Extract yesterday's date from today

//optimized way 
        var yesterday = new Date();
        yesterday.setDate(yesterday.getDate()-1);
        console.log(yesterday) // log yesterday's date 


//in-detail way 
        var today = new Date();
        var yesterday = new Date();
        yesterday.setDate(today.getDate()-1);
        console.log(yesterday) // log yesterday's date
Lucey answered 10/8, 2015 at 6:26 Comment(0)
C
-3

Date class will give the current system date and current_ date - 1 will give the yesterday date.

Eg:

var d = new Date(); // Today!
d.setDate(d.getDate() - 1); // Yesterday!
Carnify answered 2/7, 2018 at 9:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.