0

I am working on a angular project.

I want to the start time and off time of a office in a table.

Example : 8:30 - 17:30

The office has two branches in UK and India.

So user may enter time in UK time or Indian time. But I am going to store the time in GMT time.

SO I want to give an option to select the time zone and enter the time.

Then I have to convert then that time into GMT time and send to database.

How can I convert UK time to GMT time?

Or do you know any idea to do this scenario Please advice me.

Thank you

hanushi
  • 1,169
  • 2
  • 12
  • 27
  • Just the time isn't sufficient since many places have different offsets on different dates, e.g. due to daylight saving and historic offset changes. You need the date also. PS "UK time" is GMT, except in summer when it's British Summer Time instead. – RobG Aug 07 '22 at 10:27
  • Okay. I understand. The date of the day will change. So I need to store start time and off time for 365 days in database. Is is a good idea?. I thought to store start and off time for a week (thers is only 7 rows the table. It can be update later). If next week if there any changes user will update the table. And user can enter UK time or India time. Any other idea to solve this requirement? – hanushi Aug 07 '22 at 11:02
  • Is there any option in moment js to solve this ? – hanushi Aug 07 '22 at 11:06
  • I think you need [Moment Timezone](https://momentjs.com/timezone/) for that. But [Luxon](https://moment.github.io/luxon/#/) is the replacement for Moment and includes the timezone stuff. E.g. to create a date for London you might use `let ukDate = DateTime.fromISO('2017-05-15T09:10:23', {zone: 'Europe/London'});`. You can convert that to an ECMAScript time value using *toMillis*. There are other libraries that can do the same thing. – RobG Aug 07 '22 at 11:29
  • Probably a duplicate of [*How do I convert a time (string) from one timezone to another in Javascript Node.js*](https://stackoverflow.com/questions/73247942/how-do-i-convert-a-time-string-from-one-timezone-to-another-in-javascript-node). – RobG Aug 08 '22 at 03:11

2 Answers2

1

ECMAScript Date instances don't have a timezone, they are inherently UTC. System settings are used for default toString plus get and set methods so they appear to be local.

Also, the parser is very basic and is generally to be avoided other than for parsing the exact format specified for toISOString.

You should also see How to initialize a JavaScript Date to a particular time zone, which might be a duplicate for this question.

The best way to achieve what you're after (until the Temporal object is widely supported) is to use a library. There are a number of libraries that work with timezones, the following uses Luxon.

// Alias
let DateTime = luxon.DateTime;

// Create a date for now in London
let ukDate = DateTime.now().setZone('Europe/London');

// Set it to the required time
let ukOpenTime = ukDate.set({hour:8, minute:30, second:0, millisecond: 0});
console.log('ukOpenTime as string    :' + ukOpenTime.toString());

// Get time value to store
let millis = ukOpenTime.toMillis();
console.log('ukOpenTime as time value: ' + millis);

// Show equivalent local time in numerous ways
// Shift ukOpenTime to local zone: default is the local (system) timezone
let localOpenTime = ukOpenTime.setZone();
console.log('Equivalent local time 1 : ' + localOpenTime.toString());

// Use the time value (millis) to create a new Luxon object
let localOpenTime2 = DateTime.fromMillis(millis);
console.log('Equivalent local time 2 : ' + localOpenTime2.toString());

// Use the time value (millis) to create a plain date
let localOpenTime3 = new Date(millis);
console.log('Equivalent local time 3 : ' + localOpenTime3.toString());
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/3.0.1/luxon.min.js"></script>
RobG
  • 142,382
  • 31
  • 172
  • 209
0

why not store it in Epoch Time and convert it every time? like 1659755549? or use epoch as intermediate for calculation

p.s. no rep for comments :(

Gergo
  • 93
  • 1
  • 7
  • can you explain more about the calculation. I am confusing How to convert to a common time from any timezone . If I get any zone time from user I have to cnvert a commonTime and stroe into DB noh?. – hanushi Aug 06 '22 at 07:45
  • I have not used the Epoch time recently, but quick googling shows following link. I think that JavaScript will always use the Epoch and then convert it to the time zone https://www.epochconverter.com/programming/ also the following thread might be usefull https://stackoverflow.com/questions/4631928/convert-utc-epoch-to-local-date – Gergo Aug 06 '22 at 15:15
  • Please don't use answers for comments. In ECMAScript, "epoch time" (i.e. offset from 1 Jan 1970 UTC in milliseconds) is called a time value, and is inherently UTC. The OP wants to know how to convert a time to GMT (which is essentially identical to UTC), not from UTC. – RobG Aug 07 '22 at 10:31
  • I did not have enough reputation for comments. Yes. some people are still with low reputation and 50 reputation is required to post comment. Anyway- your answer also does not show how to parse "7:30 PM 8/10/2022" or "19:30 2022-08-10" string and then to convert it to time. When I get your reputation- then maybe I will edit the question :p – Gergo Aug 10 '22 at 16:48