GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — new Date() does not recognize particular date string · Issue #7583 · NativeScript/NativeScript

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

New issue
Copy link
New issue
Copy link
Closed
Closed
new Date() does not recognize particular date string#7583
Copy link
Labels
os: iosquestion

Description

@mandrag0ra
mandrag0ra
opened on Jul 25, 2019
Issue body actions

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
    added
    question
    os: ios
    on Jul 25, 2019
  2. NickIliev commented on Jul 25, 2019

    @NickIliev
    NickIliev
    on Jul 25, 2019
    Contributor
    More actions

    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.

  3. NickIliev
    closed this as completedon Jul 25, 2019
  4. NathanaelA commented on Jul 26, 2019

    @NathanaelA
    NathanaelA
    on Jul 26, 2019 · Hidden as abuse
    show commentMore actions
  5. mandrag0ra commented on Jul 29, 2019

    @mandrag0ra
    mandrag0ra
    on Jul 29, 2019
    Author
    More actions

    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

    Labels

    os: iosquestion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.