-2

I have a Date in UTC format, and want to get the hour and minute but in the local time zone the browser is running in - and without me specifying the timezone explicitly, just using the default used by the browser. I know I can get the whole date time in the local zone as a string using Date.toLocaleTimeString() or Date.toString() but don't want to have to parse out the hour and minute.

Also I can get the numeric hour and minute from a Date using Date.getHours() and getMinutes(), but this won't be in the local time zone - or will it? (ironically my local timezone is UTC so not sure how to test)

Thanks

EDIT: Answering my own question, see below

Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
Alex Kerr
  • 956
  • 15
  • 44
  • Does this answer your question? [Convert date to another timezone in JavaScript](https://stackoverflow.com/questions/10087819/convert-date-to-another-timezone-in-javascript) – about14sheep Jan 08 '22 at 18:39
  • 2
    *"using Date.getHours() and getMinutes(), but this won't be in the local time zone - or will it?"*: it will, but make sure you used a correct way to interpret the UTC string to make the Date instance. Just add a snippet with example input and code, so we can confirm that part of the code. – trincot Jan 08 '22 at 18:41
  • Also, [answers in this question](https://stackoverflow.com/questions/16448754/how-to-use-a-custom-time-in-browser-to-test-for-client-vs-server-time-difference) give interesting info on how to test with a different time zone than your own. – trincot Jan 08 '22 at 18:47
  • how is your `I have a Date in UTC format` exactly, in a string ? show us a sample! – Mister Jojo Jan 08 '22 at 18:50
  • Thanks all, I've answered my own question below. Thanks for your suggestions. – Alex Kerr Jan 10 '22 at 20:16

3 Answers3

0

I would use toLocalstring and then i would convert string to number.

function getNumericHourAndMinute() {
  const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
  const d = new Date().toLocaleString('en-US', { timeZone: tz,  timeStyle: 'short', hour12: false });
  const p = d.split(':');
  
  return [Number(p[0]), Number(p[1])];
  
}
console.log(getNumericHourAndMinute());
Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
0

You can use Date.prototype.getHours and Date.prototype.getMinutes to get the hours and minetes from a string date like: 2022-01-08 14:00:00.

Use this code:

var d = new Date(/* A example date: '2022-01-08 14:00:00'*/);

var h = d.getHours();
var m = d.getMinutes();

// Whit the example date you get in the console: 'Time: 14:00.'
console.log('Time: '+ h + ' : ' + m + '.');
Marcos-MD
  • 86
  • 9
0

So a Javascript Date object is apparently not in a timezone as such, it's basically just the number of milliseconds since the Unix Epoch as per Date.now() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now

So what I didn't realise until I checked the docs ;-) was that Date.getHours() returns the numeric hours value according to local time (i.e. time in the timezone of the device running the code) and not UTC time (which I was trying to avoid). https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getHours

So Date.hours() gives me exactly what I want (if for some reason I did want UTC, I could use Date.getUTCHours() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCHours

Then I can just use Date.getMinutes() to get the minute value of the Date and that's not affected by timezone anyway so all good :)

Alex Kerr
  • 956
  • 15
  • 44