Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
How to get hour and minute as numeric values (not string) in local timezone from Javascript Date object?

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

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

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());

about 4 years ago · Juan Pablo Isaza Report

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 + '.');
about 4 years ago · Juan Pablo Isaza Report

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 :)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!