How to convert obtuse string to a javascript Date in Angular 12? - javascript

While using Angular 12 I have a date as a string in a very custom format, from which I need to produce a Date object. An example of the date strings I have are:
20280328
20271201
To the human eye, these are obviously in the format yyyyMMdd however none of the standard parse functions appear to understand this (not unexpected.)
Other than simply breaking the string using substring, is there a better way to interpret these strings into a Date object?
I was hoping to be able to provide a custom input format for the .parse() function, but this does not appear to be available.

Related

toISOString not working in firefox

I am creating a new date toISOString -
new Date(03-13-2016 00:00).toISOString();
This works fine in IE and Chrome however NOT in FireFox.
I have tried to modify the string slightly like -
new Date(03-13-2016T00:00:00Z).toISOString();
However this also failed. How can I achieve the desired result to work across all browsers?
2016-03-13T00:00:00.000Z
PS I am aware I start with a string then try and create a string with the toISOString - reason being this handles timezone offset to UTC in one line which is required.
When you pass a string to the Date constructor, it internally calls Date.parse to attempt to get a valid date from it. This first checks to see if it is one of the Date Time formats in the specification. If not (and both "03-13-2016 00:00" and "03-13-2016T00:00:00Z" aren't), the parse specification goes on to say:
If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats. Unrecognizable Strings or dates containing illegal element values in the format String shall cause Date.parse to return NaN.
In this case, it seems both IE and Chrome have code in place that allows it to be correctly parsed, while Firefox doesn't. The only way you're really going to fix this is to have a string that conforms to the specification, or to call the constructor with individual date/time component parts.
If you don't mind pulling a library in or need to work with dates more often, use moment.js which has some very convenient date and time methods and is cross browser compatible.
Your string could then be converted to an ISO String like:
moment('03-13-2016 00:00', 'MM-DD-YYYY HH:mm').format();

javascript: convert date and time strings to UTC

I have had a truly gnarly time trying to work with Date objects in javascript, which strike me as ugly and unintuitive.
I am getting back two objects from an internal API: one date string in the form of YYYY-MM-DD and one time string in the form of HH:MM.
What I want to do, in javascript, is to merge these into a UTC-formatted string of the form YYYY-MM-DDTHH:MM:00Z. I have been playing around a bit with the moment.js and moment timezone libraries, but I'm not sure how I might leverage these to complete this task.
I solved this thusly using moment timezone:
dateTime = date.concat(" ").time
dateTimeAdjusted = moment.tz(dateTime, timezone).format()
(where timezone was a variable that I had access to that contained the timezone of the given date and time.)

Manipulate DateTime and retain original format

I have a DateTime string which I can assume is in a parsable format using Date.parse() or a 3rd party library. The format could differ based on a user's location and settings. For example, here are some strings I could see: "2014/08/19 16:10:20" or "08/19/2014 4:10:20 PM".
I would like to manipulate the time, say by adding 30 minutes, but also preserve the original format string when I present the new time to the user. I suppose I'm looking for something like a parse function that gives you a DateTime along with the format string it detected. Is there an easy way to accomplish this? 3rd party libraries are welcome, with a preference towards jQuery since I'm already using that.
Note: I don't know what the format is going to be before I parse the datetime string. It can be one of many different formats. (However I probably could build a list of possible format strings.)

Validate a date in Nodejs with MomentJs

I want to validate a date in a NodeJS application.
I tried the MomentsJs library, but it seems that the isValid() function ignored all alpha characters, it's too tolerant for me (When I specify the dateformat as YYYY-MM-DD, I expect that the date is a 10-characters-string.):
moment("One,2 and 011, 12-10", "YYYY-MM-DD").isValid() // returns true instead of false
moment("Seppl"); // parsing seems to work with all values?
Is there any way to check dates more strictly?
How can I check if parsing a date fails? (It returns -62167222800000 when printing wrong dates it).
Is there any other better Javascript Data lib that work with different date formats and support date manipulations like MomentJs?
Moment.js library doesnt provide any method validate the date with given format.
moment("<>", dateFormat) will just check the given date but not with given format.
Following lines from momentjs.com
Note: It is not intended to be used to validate that the input string matches the format string. Because the strictness of format matching can vary depending on the application and business requirements, this sort of validation is not included in Moment.js.
http://momentjs.com/docs/#/parsing/is-valid/

Format javascript date to match rails format

I get a date back from rails that looks like:
"2010-10-29T00:00:00+00:00"
And I'd like to convert a javascript date created by 'new Date()' to that format. Is there an easy way?
You're looking for ISO 8601 format.
I've always been fond of the Date.js Library for any date manipulation/formatting in JS.
you can use the the toISOString() method to get this format
using dateformat you can format date as you want
TRY these
http://blog.stevenlevithan.com/archives/date-time-format
http://jacwright.com/projects/javascript/date_format
http://www.mattkruse.com/javascript/date/
Looks like you're trying to parse an ISO8601 date string. A quick google search returned a function at dansnetwork.com that claims to be able to parse ISO8601 strings into Date objects. Or you could try parsing it yourself (Check out String.split(), and the Date api.). Luckily for you, I believe I read somewhere that Mozilla's Javascript 1.8.5 is going to support ISO8601 strings in its native parse() function. Hopefully the rest will follow suit shortly.

Categories