-1

I am using Javascript for Parse.com Cloud Code. According to Parse.com they return UTC time. I am in EDT (GMT -4)

I'm trying to get today's date at midnight to no avail. Here is my code:

var date = new Date();
var startDay = Math.floor((date.setUTCHours(4,0,0,0) / 1000));

So before 8pm on each day, the code returns today's date at midnight which is what I want. However, after 8pm it returns tomorrow's date at midnight. I believe the reason is due to the UTC date changing to Today+1 at midnight. But I cant figure out how to resolve this issue in a way that I get my local date at midgnight.

PS: I also tried setHours(4,0,0,0) in vain. If I use setUTCHours(0,0,0,0) it returns today's date at 8pm

Thank you for your help.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
user3522242
  • 85
  • 2
  • 6
  • I googled your exact title. My first hit was an SO question asking exactly this. Please do your own research, google your question title, and close your question as a duplicate. – djechlin May 19 '14 at 01:06
  • `always give the asker the benefit of the doubt`...no it's on you to explain what you've tried – charlietfl May 19 '14 at 01:12
  • Guys, give the OP (myself in this case) the benefit of the doubt before you jump to bashing conclusions. If you saw anything like this in the first 5 pages of Google, rest assured I tried it. This is not Javascript vs Browser. I specifically said Parse.Com To whomever tagged this as duplicate, the solution you linked to is something I tried already and I said it in my post. Please read carefully before playing cops. – user3522242 May 19 '14 at 01:12
  • @charlietfl did you read my PS before you posted that comment? – user3522242 May 19 '14 at 01:16
  • I came here to seek help from good people, turns out I ended up in trolls-land. Very unfortunate indeed. If one can't understand the question and pay close attention to language subtleties (i.e. @djechlin), one needs to refrain from commenting. This is supposed to be a constructive community not a youtube comment board. – user3522242 May 19 '14 at 02:27
  • I couldn't agree more. Please make it into a more constructive community by researching your questions thoroughly. This also isn't supposed to be yahoo answers, and tons of users dumping the same questions with no prior research makes it exactly that. If this is your question quality, I would rather troll you away, and for the record, I told you *exactly* how to solve your problem in a way that teaches you to avoid asking poor questions here (that will be met with this rancor going forward also). – djechlin May 19 '14 at 02:34
  • @djechlin Although I believe your intention is good, your comments are far from constructive. For the N'th time, your link is NOT the solution to my problem. The keyword in my question is Parse.com. If your ego isnt letting you recognise your inappropriate behaviour, it's your prerogative. I urge you to refrain from trolling and let the good people with Parse.com experience help if you really meant for SO to be a constructive community as you claimed. – user3522242 May 19 '14 at 04:03
  • It seems to me that it's nothing to do with Parse.com and that date() is doing exactly what you are asking it to. Since you seem OK to hardwire the time difference, just check for 8pm and adjust it yourself? Also ": I also tried setHours(4,0,0,0) in vain." is not maximally helpful since you've given us no clue about what that returns. – The Archetypal Paul May 19 '14 at 08:19
  • Thanks for your response @Paul. I m inclined to believe it has to do with Parse because when I put the same code on a regular webserver it works. Anyway, I look for answers elsewhere. – user3522242 May 19 '14 at 08:57

1 Answers1

5

It's doing everything correct.

Think about it from UTC's point of view. setUTCHours(0,0,0,0) will always set it to midnight that day.

From your point of view though (GMT-4), setting the date to midnight (UTC) at 8pm (GMT-4) appears to go a day forward.

This is because when you call date.setUTCHours(), it first converts that time to UTC, then sets the hours. If you create the date on May 18th, 8pm GMT-4, then call setUTCHours(), it will first convert that date to May 19th, 12am UTC. Then it will perform the hour change.

Here's what you can do to make sure it returns midnight on the same day as the user's timezone:

const date = new Date();

// Convert to midnight in your timezone first
date.setHours(0,0,0,0);

// Convert to midnight UTC
date.setUTCHours(0,0,0,0);
gregnr
  • 1,222
  • 8
  • 11
  • Thanks @gregnr. I apologize if it's not clear in my post but I ve done that already and it returns today at 8pm. Also, there is no user's timezone as the result are not for browser consumption. Data is handled in Parse.com Cloud Code – user3522242 May 19 '14 at 01:58
  • I think we need to know more about how you run this code. When you're testing, do you run the code on their servers? How is the result getting returned for you to see? – gregnr May 19 '14 at 02:11
  • The code runs on their servers and results are populated in a class they host which I can retrieve via REST. That's why I specified Parse.com in my post hoping someone who has experience with their service might help. – user3522242 May 19 '14 at 02:19