Convert ISO String to PST using Moment.js - timezone

I am trying to convert an ISO String to PST time. For some reason, it is converting it to my local timzone instead of to PST. The PST for this string below is "7:40 PM" for reference. Here is the code I am running:
var now = moment('2018-07-09T02:40:38.818Z').tz('America/Los_Angeles').format('MM-DD-YYYY h:mm:ss a');
alert(now);
JS Fiddle w/ Moment installed:
https://jsfiddle.net/john23/5gbv2csj/5/

Related

DateFormatter.date(from: String) vs Locale

I'd like to ask for some help understanding what might cause my DateFormatter to fail...
App receives data from server as string (ex. "2020-12-13 02:54:36 UTC")
App parses string to date
App supports English and Spanish localisations (irrelevant?)
Code:
DateFormatter (all setup):
dateFormat = "yyyy-MM-dd HH:mm:ss zzz"
generally all dates are returned in UTC but using 'zzz' to also cover other time formats like CDT etc.. we had some mix up on sever side at one point...
Problem:
I've received a single log from a user (using Spanish locale) who experienced date parser failure. But i can't reproduce this anyhow... (phone set to EN / ES - dates parse ok for both UTC and CDT time zones).
Server Response:
{ "creation_date":
"2021-05-14 06:07:55 UTC", }
where date(from string:) returns nil for above date(s).
Am I doing this wrong?
Improvements? (i.e. would setting formatter.locale = "en_EN" help? Server date format is fixed.

Why create Date from String only failed On Real iphone?

I'm using following function to create Date from String. It works well on simulator. But it crashed on real iPhone.
String: "Tue May 23 23:19:41 +0800 2017"
The first picture is debugging information on real iPhone. The second one is debugging information on simulator.
func createDate(fromString string: String) -> Date {
let formatter = DateFormatter()
formatter.dateFormat = "EEE MMM dd HH:mm:ss zzz yyyy"
let date = formatter.date(from: string) //fatal error: unexpectedly found nil while unwrapping an Optional value
return date!
}
I even tried it on playground. It's really weird!
Thanks!
this link may solve your problem.....
Swift
formatter.locale = Locale(identifier: "en_US")
I bet it's crashing on the next line, not on the line you've commented. You're force-unwrapping the result. That force-unwrap will crash with the exact error you are reporting if the date conversion fails.
I call the ! operator the "crash if nil" operator. You should not do that. You need to program defensively and return the optional, then write the calling code to handle the case where the conversion fails.
Others have already pointed out that date formatters depend on the locale of the device, and if it's different your conversion could fail. Force the formatter's locale to a known locale if you want to give it literal strings who's format doesn't vary based on country and language.
from the app docs
When working with fixed format dates, such as RFC 3339, you set the
dateFormat property to specify a format string. For most fixed
formats, you should also set the locale property to a POSIX locale
("en_US_POSIX"), and set the timeZone property to UTC.
RFC 3339
In macOS 10.12 and later or iOS 10 and later, use the
ISO8601DateFormatter class when working with ISO 8601 date
representations.
wiki ISO 8601
For proper format, use Date Field Symbol Table
you date format slightly wrong, please use the bellow example dateformat.
let dateString : String = "Tue May 23 23:19:41 +0800 2017"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE MMM dd HH:mm:ss ZZZ yyyy"
dateFormatter.timeZone = TimeZone(identifier: "GMT")
print(dateFormatter.date(from: dateString))

Function to Convert UTC DateTime to GMT Time In Delphi XE5

Please I Need a Function to Convert a UTC DateTime to GMT Format.
This Function Should Receive the UTC DateTime as a TDateTime and Return The GMT Time as a String.
Google Does Not Seem to Help Much.
Here is an Illustration
Current Value in UTC DateTime := 7/9/2014 11:36:51 PM
Needed Value in GMT Time := 2014-07-09 23:36:51
Delphi Version : XE5
Thanks.
Use FormatDateTime for this.
The documentation describes the format string options in detail. The format string that you need is:
yyyy-mm-dd hh:nn:ss

I don't understand UTC date formatting using DateFormat in Dart

When UTC date is formatted by DateFormat#format(), I expect to get a String of UTC date formatted, but not.
Please look at the code below. These tests are passed.
My question is why does DateFormat#format() return a time of 12:00 past UTC date? What am I missing here?
Date date(int millisecondsSinceEpoch, bool isUtc) =>
new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, isUtc:isUtc);
DateFormat df = new DateFormat("yyyy-MM-dd hh:mm");
// JST (+9.00)
expect(date(0, false).toString(), equals("1970-01-01 09:00:00.000"));
expect(df.format(date(0, false)), equals("1970-01-01 09:00"));
expect(date(0, true).toString(), equals("1970-01-01 00:00:00.000Z"));
// Why 12 o'clock?
expect(df.format(date(0, true)), equals("1970-01-01 12:00"));
Your format string you've written uses lowercase hh which is a 12 hour clock. Use uppercase HH for the hours to get a 24-hour clock. The output you are getting in the last line is currently saying 12 midnight, rather than 00 hours in a 24-hour clock.
See the explicit pattern syntax in DateFormat

DateTimeFormat.parse() failure

Code:
DateTimeFormat dateFormat = DateTimeFormat
.getFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date date = dateFormat.parse("Wed May 30 18:33:22 PDT 2012");
Result:
Uncaught java.lang.IllegalArgumentException: Wed May 30 18:33:22 PDT 2012
Why?
I've checked and doublechecked my pattern against the docs. Plugging the pattern into a SimpleDateFormat tester gives the expected results as well, although that's obviously not a conclusive test.
Parsing of the date has limited support as per the docs you mentioned:
The time zone support for parsing is limited. Only standard GMT and RFC format are supported. Time zone specification using time zone id (like America/Los_Angeles), time zone names (like PST, Pacific Standard Time) are not supported.
More specifically
In the current implementation, timezone parsing only supports
GMT:hhmm, GMT:+hhmm, and GMT:-hhmm.
So, the following code works fine:
DateTimeFormat dateFormat = DateTimeFormat
.getFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date date = dateFormat.parse("Thu May 31 09:45:21 GMT-07:00 2012");
GWT.log("Date "+date);

Resources