When testing my code that uses moment.utc I have some tests that use bad format: moment.utc('13-06-2018T13:00:00'). When doing this, I get this warning:
Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
Why is that? I guess moment.utc is shorthand that is using UTC format under the hood, and I don't see a method version that accepts format so I cannot get rid of this warning.
How can I parse valid/invald utc string with moment.utc so I don't get this warnings?
As the warning and the linked guide, you have to take a look at moment(String) docs and use moment(String, String) for parsing strings not in ISO 8601 or RFC 2822 format.
When creating a moment from a string, we first check if the string matches known ISO 8601 formats, we then check if the string matches the RFC 2822 Date time format before dropping to the fall back of new Date(string) if a known format is not found.
For consistent results parsing anything other than ISO 8601 strings, you should use String + Format.
Here a live example:
console.log( moment.utc('13-06-2018T13:00:00', 'DD-MM-YYYY[T]HH:mm:ss').format() );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
Related
I am seeing error the input "06/09/22 02:14 CDT" can't be parsed as format MM/dd/yy HH:mm ZZZZ` when trying to get luxon date time from string.
DateTime.fromFormat("06/09/22 02:14 CDT","MM/dd/yy HH:mm ZZZZ")
Not sure what is the valid format I need to use when there is time zone in date string.
Thanks.
Issue is that you input contains CDT that is not recognized by Luxon since ZZZZ is not a valid token as explained in the Parsing -> Limitations section of the docs:
Not every token supported by DateTime#toFormat is supported in the parser. For example, there's no ZZZZ or ZZZZZ tokens. This is for a few reasons:
Luxon relies on natively-available functionality that only provides the mapping in one direction. We can ask what the named offset is and get "Eastern Standard Time" but not ask what "Eastern Standard Time" is most likely to mean.
Some things are ambiguous. There are several Eastern Standard Times in different countries and Luxon has no way to know which one you mean without additional information (such as that the zone is America/New_York) that would make EST superfluous anyway. Similarly, the single-letter month and weekday formats (EEEEE) that are useful in displaying calendars graphically can't be parsed because of their ambiguity.
You can add fixed string 'CDT' in your format or remove it completely from your input. You can use zone option (America/New_York in the example above, or America/Chicago in your use case) of DateTime#toFormat to take into account timezone offset.
Example:
const DateTime = luxon.DateTime;
const dt1 = DateTime.fromFormat("06/09/22 02:14 CDT","MM/dd/yy HH:mm 'CDT'", {zone: 'America/Chicago'})
const dt2 = DateTime.fromFormat("06/09/22 02:14", "MM/dd/yy HH:mm", {zone: 'America/Chicago'})
console.log(dt1.toISO());
console.log(dt2.toISO());
<script src="https://cdn.jsdelivr.net/npm/luxon#2.4.0/build/global/luxon.min.js"></script>
Has anyone gotten any meaningful use out of datetime.tryparse? I'm trying to accept date formats such as...
MM/dd/yyyy
MM-dd-yyyy
MM.dd.yyyy
it seems DateTime.tryParse always returns null for all of these formats. Is there a library or a more convenient way to accept date times of different formats.
DateTime.[try]parse only parses a very distinct format, namely:
a subset of ISO 8601 which includes the subset accepted by RFC 3339
To parse formats like 06/09/2019 use the DateFormat class from package:intl.
DateFormat.yMd().parse('06/09/2019'); // defaults to en_US, i.e. MM/dd/yyyy
This code:
import 'package:intl/intl.dart';
main() {
print(DateFormat.yMd().parse('06/09/2019'));
}
prints
2019-06-09 00:00:00.000
as expected
It is worse than you think:
DateTime.tryParse("2022-02-34")
DateTime (2022-03-06 00:00:00.000)
So for invalid input (format is correct but not the date) Dart knows better and retuns what it thinks is a valid date.
With invalid input there is no valid date
I am just printing the ISO datetime with timezone as per the below documentation
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a003169814.htm
This is my code
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("yyyy-mm-dd'T'hh:mm:ss.nnnnnn+|-hh:mm");
df.setTimeZone(tz);
dateTimeWithTimeZone = df.format(new Date());
However i am getting this exception
Illegal pattern character 'n'
I cant use this format directly in Java ?
java.time
dateTimeWithTimeZone = Instant.now().toString();
System.out.println(dateTimeWithTimeZone);
When I ran this snippet just now, I got this output:
2019-03-18T22:28:13.549319Z
It’s not clear from the page you link to, but it’s an ISO 8601 string in UTC, so should be all that you need. I am taking advantage of the fact that the classes of java.time produce ISO 8601 output from their toString methods. The linked page does show the format with hyphens, T and colons (2008-09-15T15:53:00+05:00), it shows another example with decimals on the seconds (15:53:00.322348) and a third one with Z meaning UTC (20080915T155300Z), so I would expect that the combination of all three of these would be OK too.
The format you used in the quesiton seems to try to get the offset as +00:00 rather than Z. If this is a requirement, it’s only a little bit more complicated. We are using an explicit formatter to control the variations within ISO 8601:
DateTimeFormatter iso8601Formatter
= DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSSSSxxx");
dateTimeWithTimeZone = OffsetDateTime.now(ZoneOffset.UTC).format(iso8601Formatter);
System.out.println(dateTimeWithTimeZone);
2019-03-18T22:28:13.729711+00:00
What went wrong in your code?
You tried to use the formatting symbols from your source with SimpleDateFormat. First, you should never, and especially not in Java 8 or later, want to use SimpleDateFormat. That class is notoriously troublesome and long outdated. Second, some of its format pattern letters agree with the symbols from your source, some of them don’t, so you cannot just use the symvol string from there. Instead you need to read the documentation and find the correct format pattern letters to use for year, month, etc. And be aware that they are case sensitive: MM and mm are different.
Link
Oracle Tutorial: Date Time
explaining how to use java.time.
I am using moment.js in my application and the expected date format is
2017-01-09T17:05:00.000 //Expected Result
Where as if i call
moment().format()
I am getting ISO 8601 format i.e with T and minus sign
(2017-01-14T17:05:00-06:00) // Actual result.
What should i use to get this format of with .000
ISO 8601 is the default representation of moment's format() API. But you can customize it by passing required format pattern.
In your case, moment().format('YYYY-MM-DD[T]HH:mm:ss.SSS') will produce what you're looking
2017-01-10T11:55:56.621
Find more customization options here: http://momentjs.com/docs/#/displaying/
--
Another option to know, though it works only for UTC time : moment().toISOString()
2017-01-10T06:56:18.465Z
[credit: Rajesh]
I get this value from my backend service: 171054. It represents hh:mm:ss. But when I use the formatting options from the docs it gives me back 00:00:00.
Things I've tried:
moment('171054').format('hh-mm-ss')
moment('171054').format('HH-mm-ss')
moment('171054').format('HH-MM-SS')
You are confusing format option with parsing option. Since your input string is not in ISO 8601 format, you have to specify format when parsing.
Here a working example for your use case:
var mom = moment('171054', 'HHmmss');
console.log(mom.format());
console.log(mom.format('HH:mm:ss'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script>
I am not sure if momentjs can read that as date since there is no identifier. I would suggest changing the value for example you have 171054, get each 2 digits since you sure that this is represent as hh:mm:ss then add identifier between then like ":" or "-" then try us momentjs formatting again.