Home > Blockchain >  How get UTC of start of day for specific timezone?
How get UTC of start of day for specific timezone?

Time:12-04

I have to convert 2022-11-29 to '2022-11-29T04:00:00.000Z' it is offset for Santo Domingo Timezone.

But the first problem StartFromUtc is already '2022-11-29T02:00:00 02:00' but I expected '2022-11-29T00:00:00 00:00'.

so the next calculation is wrong too.

How can help?

const tz = 'America/Santo_Domingo';
const startFromDate = '2022-11-29';
const utcdate = dayjs(startFromDate   'T00:00:00.000Z');
const tzdate = utcdate.tz(tz);
const utcFromTzdate = utcdate.tz(tz);

console.log(
  'StartFrom: ', startFromDate, 
  '\nStartFromUtc: ', utcdate.format(), 
  '\nCreated UTC:  ', utcdate.toISOString(), 
  '\nSanto Domingo:', tzdate.format(),
  '\nUTC For Santo Domingo:', utcFromTzdate.format(),
);
<script src="https://cdn.jsdelivr.net/npm/dayjs@1/dayjs.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dayjs@1/plugin/utc.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dayjs@1/plugin/timezone.js"></script>
<script>
  dayjs.extend(window.dayjs_plugin_utc);
  dayjs.extend(window.dayjs_plugin_timezone);  
</script>

CodePudding user response:

Given a timestamp in YYYY-MM-DD format, dayjs assumes UTC (perhaps to be consistent with ECMA-262), so it can be parsed to zero hours UTC using:

dayjs(startFromDate);

To convert it to some other timezone without shifting the date and time values, add true as the second parameter when calling tz:

let tzdate = dayjs(startFromDate).tz(tz, true)

Then get the equivalent UTC date and time using tz again:

let utc = tzdate.tz('UTC')

E.g.

const tz = 'America/Santo_Domingo';
const startFromDate = '2022-11-29';
let tzdate = dayjs(startFromDate).tz(tz, true);
let utc = tzdate.tz('UTC');

console.log(
    'StartFrom     : ', startFromDate, 
  '\nSanto Domingo :', tzdate.format(),
  '\nUTC eqiuvalent:', utc.format(),
);
<script src="https://cdn.jsdelivr.net/npm/dayjs@1/dayjs.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dayjs@1/plugin/utc.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dayjs@1/plugin/timezone.js"></script>
<script>
  dayjs.extend(window.dayjs_plugin_utc);
  dayjs.extend(window.dayjs_plugin_timezone);  
</script>

CodePudding user response:

Try this:

const tz = 'America/Santo_Domingo';
const startFromDate = dayjs(new Date('2022-11-29 UTC'));
const tzdate = startFromDate.tz(tz);
const utcFromTzdate = startFromDate.tz('UTC');

console.log(
  'StartFrom: ', startFromDate, 
  '\nStartFromUtc: ', startFromDate.format(), 
  '\nCreated UTC:  ', startFromDate.toISOString(), 
  '\nSanto Domingo:', tzdate.format(),
  '\nUTC from Santo Domingo:', utcFromTzdate.format(),
);
<script src="https://cdn.jsdelivr.net/npm/dayjs@1/dayjs.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dayjs@1/plugin/utc.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dayjs@1/plugin/timezone.js"></script>
<script>
  dayjs.extend(window.dayjs_plugin_utc);
  dayjs.extend(window.dayjs_plugin_timezone);  
</script>

  • Related