React Native New Date string - javascript

I am using react-native-calendar-picker. Initially when my app data loads, if there isn't a date stored that was picked, I assign the specific array with new Date() which returns something like this: Thu Feb 18 2016 22:58:12 GMT-0700 (MST). Once the user updates the date I store the new date in React Native's AsyncStorage which has a value something like this: Mon Feb 29 2016 00:00:00 GMT-0700 (MST)
However, once the app reloads the date value is returned like this: "2016-02-29T07:00:00.000Z". The react-native-calendar-picker uses the date formatted like: Mon Feb 29 2016 00:00:00 GMT-0700 (MST)
Is there a way to get the date formatted back to this way?

Try,
String formatted = new Date("2016-02-29T07:00:00.000Z").toString();
Precisely be aware of data types,
new Date() would return a Date object. (2016-02-19T13:42:17.975Z)
new Date().toString() would return a formatted date string. (Fri Feb 19 2016 19:12:17 GMT+0530 (IST))

Related

moment timezone js returns wrong datetime

I am using Moment Timezone to get current datetime for Asia/Tokyo timezone.
The code is as following
var currentDateTime = new Date(moment().tz('Asia/Tokyo').format('YYYY-MM-DDTHH:mm:ss'))
Supposed that the current datetime for my local timezone is as following
Thu Jul 22 2021 19:49:47 GMT+0700 (Indochina Time)
I expected the current date for Asia/Tokyo timezone would be as following
Thu Jul 22 2021 21:49:47 GMT+0700 (Indochina Time)
In Chrome I got the expected datetime.
But in Safari on my iPhone, I got the wrong datetime.
Fri Jul 23 2021 04:49:47 GMT+0700 (Indochina Time)
It seems the current datetime returned is the right current datetime plus 7 hours.
Here is my environment
iPhone : iPhone 6
iOS : 12.5.4
The problem you have is caused by the Date constructor parsing the time according to the local timezone (in Chrome) and UTC (on Safari?).
For the time given, this code
moment().tz('Asia/Tokyo').format('YYYY-MM-DDTHH:mm:ss')
returns
"2021-07-22T21:49:47"
You then pass this time into the Date constructor:
var currentDateTime = new Date("2021-07-22T21:49:47")
Because this string has no time zone information, it is assumed to be in the device's local timezone (Chrome) or UTC (Safari) which gives the wrong date object.
While you could inject the timezone into this string (using ZZ), you can build a Date object from the moment object using:
var currentDateTime = moment().tz('Asia/Tokyo').toDate()
but this is effectively the same as
var currentDateTime = new Date()
If the intended output is a string of the format:
"Thu Jul 22 2021 21:49:47 GMT+0900 (Japan Standard Time)"
the closest you could get is:
moment().tz('Asia/Tokyo').format('ddd MMM D YYYY, h:mm:ss a [GMT]ZZ (z)')
// Thu Jul 22 2021 21:49:47 GMT+0900 (JST)
Alternatively, there is also the localized "Month name, day of month, day of week, year, time" format:
moment().tz('Asia/Tokyo').format('llll (z)')
// In my locale, I get:
// Thu, Jul 22, 2021 9:49 PM (JST)
Take a look at the Moment display format documentation for more options.

How to get `Fri Oct 25 2019` from a date object (Fri Oct 25 2019 15:27:01 GMT+0530 (India Standard Time))?

How can I get Fri Oct 25 2019 from a date object as below?
Fri Oct 25 2019 15:27:01 GMT+0530 (India Standard Time)
Use the toDateString() function from JavaScript.
let d = new Date();
// note : the actual display output depend on your browser/system settings (locale)
console.log(d.toString());
console.log(d.toDateString());
This will take the first "date only" part of the string that is displayed from the date object.
It could also be done with string manipulation, but this function is actually intended for this usecase, so probably clearer.
Doc for toDateString function

How to build Time object using a date string as Local Time

All:
Thanks for help. I wonder how can I build a Date object using local time string, for example:
If I use new Date("2016-07-01"), what I want to build is
2016-07-01 00:00:00 GMT-0800 (Pacific Standard Time) (say I am in San Francisco),
but right now, it gives me something like
Thu Jun 30 2016 16:00:00 GMT-0800 (Pacific Standard Time)
Any idea?
To convert the default UTC time created from running new Date(dateString):
const MILLISECONDS_PER_MINUTE = 60000;
const utcDate = new Date('2015-01-01');
console.log(new Date(utcDate.getTime() + (utcDate.getTimezoneOffset() * MILLISECONDS_PER_MINUTE))); // Wed Jan 01 2014 00:00:00 GMT-0700 (MST)

New date to string shows as day before because of time zone

How can I display a date to behave like that day regardless of the users time-zone?
>>> new Date('2013-09-17')
Date {Mon Sep 16 2013 20:00:00 GMT-0400 (Eastern Standard Time)}
I'm trying to use the jquery datepicker formater, however when I pass the date object it's off by a day.
How can I make sure that the users timezone is disregarded?
OK, I split the string and passed them as parameters for my expected result.
>>> d = '2013-09-17'.split('-'); new Date(d[0],d[1]-1,d[2]);
Date {Thu Oct 17 2013 00:00:00 GMT-0400 (Eastern Standard Time)}

Incorrect Javascript Date in Chrome vs Firefox

I'm getting incorrect dates in Chrome...
My code looks like this..
Title contains "2013-06-14T00:00:00", it was a DateTime in C# returned from WebAPI
As you can see here on both browsers..
When I add it to a new javascript date like this..
var dt = new Date(title)
I get different dates in different browsers...
Example - http://jsfiddle.net/RvUSq/
Looks like Firefox is assuming this datetime format without timezone is local time and Chrome/Webkit is assuming it's UTC.
If the datetime returned from the api is UTC, simply append a "Z" to the end of the string, so it becomes "2013-06-14T00:00:00Z", which indicates the time is in UTC, then you will get the same result in the two browsers.
Convert timestamp to ISO 8601 formatted string in C#, for e.g
var title = "14 JUN 2013 00:00:00" // printed from C#
Then use Date constructor
var date = new Date(title);
If you don't specify timezone the local timezone in the client machine will be set to the given time. If you specify the timezone, needed calculations will be done to convert the date to local timezone.
var title = "14 JUN 2013 00:00:00";
var date = new Date(title); // Fri Jun 14 2013 00:00:00 GMT+0530 (IST)
var title = "14 JUN 2013 00:00:00 GMT";
var date = new Date(title); // Fri Jun 14 2013 05:30:00 GMT+0530 (IST)
var title = "14 JUN 2013 00:00:00 GMT-0400";
var date = new Date(title); // Fri Jun 14 2013 09:30:00 GMT+0530 (IST)
ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse

Categories