Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide way to specify multiple formats when creating a date in UTC. #1596

Open
DavidRigglemanININ opened this issue Aug 3, 2021 · 6 comments

Comments

@DavidRigglemanININ
Copy link

If trying to parse a date in which you also need to enable it validates correctly (i.e. using strict mode), you can do so with the following

dayjs(it, ["YYYY-MM-DDTHH:mm:ss.SSS[Z]", "YYYY-MM-DDTHH:mm:ss[Z]"], true);

However, if using the UTC plugin, it doesn't seem to allow specifying multiple formats so I can only do

const date = dayjs.utc(it, "YYYY-MM-DDTHH:mm:ss.SSS[Z]", true);

It would be great the behavior of utc could be extended to support multiple formats as well. Or if anyone knows a workaround to get strict validation and UTC both working together, I'd love to hear it.

@DavidRigglemanININ DavidRigglemanININ changed the title Provide way to specify multpile formats when creating a date in UTC. Provide way to specify multiple formats when creating a date in UTC. Aug 3, 2021
@BePo65
Copy link
Contributor

BePo65 commented Sep 21, 2021

Think we should change the line 18 in dayjs/types/plugin/utc.d.ts from

export function utc(config?: ConfigType, format?: string, strict?: boolean): Dayjs

to:

export function utc(config?: ConfigType, format?: OptionType, locale?: string, strict?: boolean): Dayjs
export function utc(config?: ConfigType, format?: OptionType, strict?: boolean): Dayjs

@DavidRigglemanININ
Copy link
Author

Sorry for not responding sooner as I completely missed the notification about your message.

Anyhow, I don't think that proposed solution would help. The issue is that I need to be able to specify multiple formats, meaning that it needs to support a list (i.e. array type). The issue isn't about missing the ability to specify the locale (although others may find that helpful).

@nathansolidatus
Copy link

nathansolidatus commented May 3, 2022

I have the same issue here, it'd be really nice to have the same parameters as the dayjs() methods.

This is my workaround :

const day = dayjs(dateString, SupportedDateFormats, true)

const offset = day.utcOffset()
const parsedDate = day.utc().add(offset, 'minutes')

@BePo65
Copy link
Contributor

BePo65 commented May 8, 2022

I have traced down the issue: the problem is caused by the 'strict' parameter.
Without it, my tests run fine.
But with 'strict=true', dayjs fails to parse the input string, as long as the offset of the input string does not by chance happen to meet the offset of your current time zone.

The plugin customParseFormat tries to validate that the format of the input string has exactly the same format as one of the format strings by formatting the parsed date using the given format parameters and requires the result to match the input string.

To solve the issue, the line if (isStrict && date != this.format(format)) { must be replaced by a better comparison.
I could prepare a pull request, but I'm not sure, if this makes any sense or if it is better to contribute to the coming version 2.0 (which I would also be glad to do). @iamkun what is your recommendation?

@iamkun
Copy link
Owner

iamkun commented May 11, 2022

Thanks.

All the bugs will be tracked on v1.x.

@InfamousStarFox
Copy link

Here's what I ended up doing:

import dayjs, { ConfigType, Dayjs } from 'dayjs';
import utc from 'dayjs/plugin/utc';

dayjs.extend(utc);

const utcFix = (
    option: ConfigType,
    dayjsClass: typeof Dayjs,
    dayjsFactory: typeof dayjs
) => {
    const originalUtc = dayjsFactory.utc;

    dayjsFactory.utc = (
        config?: ConfigType,
        formats?: string | string[],
        strict?: boolean
    ) => {
        if (Array.isArray(formats)) {
            for (const format of formats) {
                const date = originalUtc(config, format, strict);

                if (date.isValid()) {
                    return date;
                }
            }

            return dayjs('Invalid Date');
        }

        return originalUtc(config, formats, strict);
    };
};

dayjs.extend(utcFix);

declare module 'dayjs' {
    export function utc(
        config?: ConfigType,
        formats?: string | string[],
        strict?: boolean
    ): Dayjs;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants