dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

Abbreviated Timezone for IST/GMT+5:30

Open rushvora opened this issue 3 years ago • 26 comments

When using the required plugins, the abbreviated timezone is not working for IST. With z, it shows GMT+5:30, but with zzz, it correctly shows Indian Standard Time.

import { Pipe, PipeTransform } from '@angular/core';
import * as dayjs from 'dayjs';
import * as advancedFormat from 'dayjs/plugin/advancedFormat';
import * as utc from 'dayjs/plugin/utc';
import * as timezone from 'dayjs/plugin/timezone';

@Pipe({
  name: 'dayjsTz'
})
export class DayJsDateTimePipe implements PipeTransform {
  public transform(value: Date, timeZone?: string, formatting = 'MMM Do YYYY, h:mm a z') {
    if (value) {
      dayjs.extend(advancedFormat);
      dayjs.extend(utc);
      dayjs.extend(timezone);
      if (timeZone) {
        return dayjs.tz(value, timeZone).format(formatting);
      }
      const userTimeZone = dayjs.tz.guess(); // here it guessed my timezone, Asia/Calcutta. I also tried hardcoding Asia/Kolkata, same result
      return dayjs.tz(value, userTimeZone).format(formatting);
    }
    return value;
  }
}

Current Output — Oct 20th 2020, 2:17 pm GMT+5:30 Expected Output — Oct 20th 2020, 2:17 pm IST

  • Day.js Version 1.9.3
  • OS: macOS Catalina 10.15.7
  • Browser Chrome 86 and Safari 14

rushvora avatar Oct 20 '20 10:10 rushvora

I think this is not an issue related to Dayjs.

The Intl object gives us this result.

iamkun avatar Oct 20 '20 14:10 iamkun

Oh wow.. seems like localisation is also needed in combination. 😐

Screenshot 2020-10-20 at 22 22 38

So, the solution would to be also to guess the locale, like we are guessing the timezone?

rushvora avatar Oct 20 '20 16:10 rushvora

So if I were to get the user locale via navigator.language, how would I use that when formatting the date? I can't see where to pass the locale for timezone purposes.

rushvora avatar Oct 20 '20 17:10 rushvora

What do you mean by 'passing locale to timezone'?

iamkun avatar Oct 21 '20 01:10 iamkun

As you can see in the above screenshot from Chrome's console, when I pass a locale to Intl.DateTimeFormat, it returns the desired abbreviation. I need to pass en-IN to get the IST abbreviation instead of GMT+5:30, similary I need to pass en-AU to get the AEDT abbreviation instead of GMT+11.

So I am hoping to be able to do the same with dayjs, and I'm figuring out if we can pass the locale which dayjs will pass to the Intl API.

rushvora avatar Oct 21 '20 07:10 rushvora

Oh, I see. Unfortunately, we do not support this ATM.

iamkun avatar Oct 21 '20 08:10 iamkun

Should I leave this issue open since I hope this support can be added or is this not the right place for feature requests?

rushvora avatar Oct 24 '20 12:10 rushvora

I'm not sure if this should be supported or not, or the proper way to support it. One question is, how can our user know the correct locale name to pass in.

iamkun avatar Oct 24 '20 15:10 iamkun

Aren't the locale names standardised?

What I feel — If no locale name is passed, then there's no need to pass any locale to the Intl API/it would work as it does now. If a locale is passed, then the same can be passed to the Intl API (like how I did in the screenshot from Chrome's console above). Do let me know if I'm mistaken about how the above can work/not work.

rushvora avatar Oct 26 '20 01:10 rushvora

Seems a good choice. I'll dig it more if I got some time.

iamkun avatar Oct 26 '20 02:10 iamkun

hello @iamkun

could you please let me know if this issue/feature is done or maybe in progress? In the doc I see it says we can use z for timezone abr https://day.js.org/docs/ru/plugin/advanced-format but it doesnt work.

Thank you!

tanyaburlakova avatar Mar 24 '21 19:03 tanyaburlakova

Hi@iamkun Any update on this thread would be very helpful :)

shubhi23994 avatar Jun 10 '21 06:06 shubhi23994

I expected such behavior like moment's zoneName() as well. https://momentjs.com/timezone/docs/#/using-timezones/formatting/

koooge avatar Jun 23 '21 16:06 koooge

Any update on this issue ? @iamkun . Thanks!

ztancinco avatar Aug 24 '21 03:08 ztancinco

@iamkun it'd be great to have any updates on the issue. thanks!

VilOstreiko avatar Sep 28 '21 10:09 VilOstreiko

Any update? Thanks @iamkun

kaangokdemir avatar Oct 06 '21 09:10 kaangokdemir

This ~~issue~~ bug should be fixed ASAP.

Until then, I worked the bug around using the following code (I don’t say it is the shortest nor fastest one, but it works).

const locale = 'en-GB'
const timezone = 'Europe/Bratislava'
const shortTimezone = new Intl.DateTimeFormat(locale, {
  timeZone: timezone,
  timeZoneName: 'short'
})
  .format(Date.now())
  .split(' ')[1]

// `shortTimezone` should be equal to 'CEST'

tukusejssirs avatar Jun 23 '22 06:06 tukusejssirs

Hi, I'm also being plagued by this issue. @tukusejssirs's solution works but this is quite the workaround to implement. It sounds like the main culprit here is being able to specify the locale?

far0s avatar Oct 28 '22 16:10 far0s

any update ?

bilalbutt044 avatar Jun 19 '23 18:06 bilalbutt044

Any news on this ?

MariusBaci avatar Dec 06 '23 21:12 MariusBaci

We can use dayjs().format('zzz').match(/\b(\w)/g)?.join("") for now.

5war00p avatar Dec 20 '23 16:12 5war00p

Please revisit this issue, thanks in advance!

itseramin avatar May 14 '24 12:05 itseramin