vendredi 11 septembre 2015

How to add 3 months to a UTC time stamp in javascript centered at midnight 00 Hours?

I am trying to add 3 months to today's date in a javascript. This is my code:

    var now = new Date();
    now.setHours(0, 0, 0, 0);;
    now.setMinutes(0);

    var plus3mo = new Date();
    plus3mo.setMonth((now.getMonth() + 3));
    plus3mo.setHours(0, 0, 0, 0);
    plus3mo.setMinutes(0);

    var utc_timestamp_today = Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0, 0);
    var utc_timestamp_3moFromNow = Date.UTC(plus3mo.getFullYear(), plus3mo.getMonth(), plus3mo.getDate(), 0, 0, 0, 0);

I want to add exactly 3 months to today's date and have the hours. It is doing that, but the catch is I need the hours to be set to exactly 00 hours or 12am. I am getting weird results with the code I have.

Here are my results that I viewed while debugging and using this UTC time stamp converter site.

1441929600000  --> 09/10/2015 20:00:00     ("Today's Date")
1449792000000  --> 12/10/2015 19:00:00     ("3 Months From Now")

As you can see, I am adding 3 months, but the hours are centered at 8pm and 7pm. Why is this happening?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire