0

I'm trying to compare this string 8/26/2019 6:53:13 which is in EST to a new Date() object to simply see if it's in the past. This works fine locally but on deployment heroku's new Date comes through in UTC format. So I had to do this hack

 if(process.env.NODE_ENV){
      timeSubmitted.setHours(timeSubmitted.getHours() + 5);  // HACK but does work
 }

I tried to get todays date and time in UTC format as an object not a string so I can compare it to other times in UTC format.

var date = new Date('2014-02-27T10:00:00')
//Thu Feb 27 2014 10:00:00 GMT-0500 (Eastern Standard Time) //this is an object


let d = moment.utc(new Date()).format()
//Does convert right now to a UTC time string, but then when I try convert it to an object

new Date(d) 
//it goes back to EST 

All together this does work, but isn't ideal because of the hardcoded number 5.

//SET DATE RANGE
const startDate = new Date();//This gets converted from EST to UTC
startDate.setMinutes(startDate.getMinutes() - 2); //Google spreadsheets saves minutes a little in the past 
//startDate = new Date(startDate.getTime() + startDate.getTimezoneOffset() * 60000);

const endDate = new Date();
endDate.setDate(endDate.getDate()  + 5)
console.log('startDate ' ,startDate,'endDate ',endDate)


  rows.forEach(row=>{

    //row.timestamp looks like this '8/26/2019 6:53:13' in EST
    var date= row.timestamp.split(" ")[0].split('/')
    var time=row.timestamp.split(" ")[1].split(':')
    var timeSubmitted = new Date(date[2], date[0] - 1, date[1], time[0], time[1], time[2]); //So this is in EST 

    //but then when deploying to heroku i had to do this hack.

    if(process.env.NODE_ENV){
      timeSubmitted.setHours(timeSubmitted.getHours() + 5);  // HACK -- It's the only way I could get this time to be in UTC/timeSubmitted = new Date(timeSubmitted.getTime() + timeSubmitted.getTimezoneOffset() * 60000);
    }


    console.log('timeSubmitted',typeof timeSubmitted, typeof startDate, timeSubmitted, startDate, timeSubmitted >= startDate,  timeSubmitted < endDate)
    if(timeSubmitted >= startDate && timeSubmitted < endDate){ //Within the date range so check if the names are in the roster 
        slackers = slackers.filter(function(a){return a.fullname !== row.whatsyourname})
    }
  })
  messageSlackers(slackers, id)
Squirrl
  • 4,909
  • 9
  • 47
  • 85
  • EST is how it is *shown* in the console. It doesn't *go back* to anything. A date object simply stores milliseconds since epoch. To get a unique point in time. It has methods to convert this value into Date, month, year or some string format to display – adiga Nov 03 '19 at 19:46
  • I think I'm looking for something like this `let oldDate = '8/26/2019 6:53:13' moment(oldDate, "MM-DD-YYYYHH:mm:ssZ").toDate();` https://stackoverflow.com/questions/11770367/use-javascript-to-convert-a-date-string-with-timezone-to-a-date-object-in-local Would that work internationally and adjust during daylight savings time? – Squirrl Nov 03 '19 at 20:45
  • I'm really confused by your question and I think that's because you've got confused somewhere. You should never, ever hardcode a timezone offset into your code. If your string input is UTC, then _tell the `Date` constructor that_ to get the proper representation. If it's some other timezone, then _tell the `Date` constructor that_ to get the proper representation. Everything else appears to be a distraction; `Date` works as you'd expect otherwise it wouldn't be fit for purpose. So perhaps all your problems stem from this hack. It's kind of hard to tell. You should narrow this down more. – Lightness Races in Orbit Nov 03 '19 at 20:55

1 Answers1

2

Timezones are just a factor taken into consideration when generating a human-readable string from a date.

But a date is a point in time, regardless of timezones. Time doesn't know about timezones! Humans invented timezones.

You're probably stringising your Date objects in some manner that uses your local timezone by default (didn't show us this code). This doesn't matter.

Comparing two Dates works. Always. You don't have to worry about some hidden timezone component ruining it. Just compare your dates and you'll see that it works fine.

tl;dr: Dates are not in a "format". Dates are dates.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055