← 输入框|github.com|刷新
Skip to content

new Date() does not recognize particular date string #7583

Closed

Description

@mandrag0ra

Environment

✔ Component nativescript has 6.0.2 version and is up to date.
✔ Component tns-core-modules has 6.0.1 version and is up to date.
✔ Component tns-android has 6.0.0 version and is up to date.
✔ Component tns-ios has 6.0.1 version and is up to date.

Describe the bug

new Date() function does not recognize this particular date string format Fri, 12 Jul 2019 08:33:59 Z
To Reproduce

// In Nativescript Core 
let date = new Date("Fri, 12 Jul 2019 08:33:59 Z")
console.log(date)
date is Invalid Date

Expected behavior

// In node v10.16.0
let date = new Date("Fri, 12 Jul 2019 08:33:59 Z")
console.log(date)
2019-07-12T08:33:59.000Z

Additional context

I also notice the same behavior in tns 5.4.2

Thank you.

Activity

  1. NickIliev commented on Jul 25, 2019

    @NickIliev
    Contributor

    I've used this Playground and noticed that the issue occurs only on iOS. That said I think the issue is related to the capabilities of JavaScript Core that are used by the iOS runtime. And following this thought, there is this SO thread that provides an explanation and a solution for using the JavaScript Date in iOS.

    Closing as the issue is not related to NativeScript but to a limitation in the JavaScript Core.

  2. NathanaelA commented on Jul 26, 2019

    @NathanaelA
  3. mandrag0ra commented on Jul 29, 2019

    @mandrag0ra
    Author

    Thank you @NathanaelA. Based on your table, I ended up using a function like the following. The fact is that I do not control the servers where the date strings come from. And I'd rather have a date, even if it's not completely accurate, instead of nothing at all. And it's far to be the perfect solution.

    As I don't want to mess with date format support by IOS either, I use it only if new Date() can not handle the date string and if I'm on IOS.

    function formatDateForIOS(dateString) {
        let struct, validIOSDate;
    
        // match "2019-01-01Z00:00:00" | Z: UTC time
        let reg1 = /^(\d{4})(?:-?(\d{2})(?:-?(\d{2}))?)?(?:Z?(\d{2}):?(\d{2})(?::?(\d{2})))$/g;
        // match "Tue, 01 Jan 2019 10:00:00 Z" | Z: UTC time but to difficult to parse has it comes from very different sources => Parse has local time :(
        let reg2 = /^(.*)? Z$/g;
        // match "2019-01-01 00:00:00" | No timezone: local time
        let reg3 = /^(\d{4})(?:-?(\d{2})(?:-?(\d{2}))?)?(?: ?(\d{2}):?(\d{2})(?::?(\d{2})))?$/g;
    
        if ((struct = reg1.exec(dateString))) {
            validIOSDate = new Date(
                Date.UTC(struct[1], struct[2] - 1, struct[3], struct[4], struct[5])
            );
            console.log(`validIOSDate is ${validIOSDate}`);
        } else if ((struct = reg2.exec(dateString))) {
            validIOSDate = new Date(struct[1]);
            console.log(`validIOS date: ${validIOSDate}`);
        } else if ((struct = reg3.exec(dateString))) {
            validIOSDate = new Date(
                struct[1],
                struct[2] - 1,
                struct[3],
                struct[4],
                struct[5]
            );
            console.log(`validIOS date: ${validIOSDate}`);
        } else {
            console.log(`${date} doe not match`);
        }
    }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions