SBB
SBB

Reputation: 8970

Javascript UTC Date Conversion

I have a function that I pass a date string to and I need to determine if that supplied date is in the past based on the current UTC Date.

I think my issue is coming from the dateToUTC function as it is returning the date at 7am UTC where the currentUTC gives me the full timestamp.

It's almost as if I need to append the hh/mm/ss to the end of the date I provide in order to check?

Any thoughts on how I can tweak this?

// I expect this to return false since the UTC DATE is not 08/17/2017
alert(isHistoricalChange('08/16/2017'))

/**
 * Determine if the start date supplied is in the past
 */
function isHistoricalChange(startDate){
    // dateToUTC(startDate) = 1502866800000
    // currentUTC() = 1502903243386
    return (dateToUTC(startDate) < currentUTC());
}


/**
 * Returns the current timestamp in UTC
 */
function currentUTC() {
  return Date.now();
}


/**
 * Returns timestamp in UTC based on date supplied
 */
function dateToUTC(d) {
  return new Date(d).getTime();
}

Fiddle: http://jsfiddle.net/981ats14/

Upvotes: 0

Views: 145

Answers (3)

RobG
RobG

Reputation: 147513

I'm not sure what you're trying to do. If you want "08/16/2017" (which I assume is 16 August, 2017) to be parsed as UTC and then to see if it is prior to the current date, then you can use a simple parse function.

The current "UTC date" is provided by new Date(), since Date objects are UTC. The host timezone setting used to calculate the current UTC time, and usually by the toString and toLocaleString methods to return a date and time in the host timezone. But the underlying value in the Date object is UTC.

So you can parse "08/16/2017" as UTC and simply compare it to new Date():

function parseMDYasUTC(s) {
  var b = s.split(/\D/);
  return new Date(Date.UTC(b[2], b[0]-1, b[1]));
}

var d = parseMDYasUTC('08/16/2017')
console.log('2017-08-16 parsed as UTC:\n' + d.toString() + 
            ' or \n' + d.toISOString());

console.log('Is "08/16/2017" UTC before today? ' +
            (parseMDYasUTC("08/16/2017") < new Date()));

The string "2017-08-16" should be parsed by the built-in parser as UTC, however string parsing is notoriously unreliable so always manually parse strings. A library can help but a simple function is usually sufficient.

Upvotes: 1

vox
vox

Reputation: 837

As you've guessed, currentUTC is returning the current datetime, not just the date. You can modify it like so to get the beginning of the day:

function currentUTC() {
    const d = new Date();
    d.setHours( 0, 0, 0, 0 );
    return d.getTime()
}

Upvotes: 0

manpozi
manpozi

Reputation: 72

You are incorrectly creating a Date object. You should call your function like this: alert(isHistoricalChange('2017-8-17')).

Below are examples of the date constructor:

var today = new Date();
var birthday = new Date('December 17, 1995 03:24:00');
var birthday = new Date('1995-12-17T03:24:00');
var birthday = new Date(1995, 11, 17);
var birthday = new Date(1995, 11, 17, 3, 24, 0);

To read more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Upvotes: 0

Related Questions