Validate a date in Nodejs with MomentJs - javascript

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/

Related

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

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.

Where does the date time format coming when use date toLocaleString?

I'm facing some date time formatting related issue.
I'm confused about how a date object's output string is formatted. I did some testing in debug, when I call the toLocalString, the output is not following locale settingin the OS .
Below is the output of the method:
"1/12/2015, 8:12:12 PM"
But what I did in the os locale setting is
Why is toLocaleString formatting the date this way? where does those format coming from?
Where to change the format setting browser is using?
Why is toLocaleString formatting the date this way?
toLocaleString() doesn't watch for user's locale formatting settings before returning the string.
Where does those format coming from?
The format is based on the conventions of user's time zone for representing date and time. So, format is machine independent.
Where to change the format setting browser is using?
As stated the format is implementation dependent. It won't help you anything. And I think browsers don't provide such functionality.
For reference I have included it's documentation below.
The Documentation of Date.toLocaleString() as mentioned in Javascript: The Definitive Guide says:
Returns
A string representation of the date and time specified by date. The date and time are repre- sented in the local time zone and formatted using locally appropriate conventions.
Usage
toLocaleString() converts a date to a string, using the local time zone. This method also uses local conventions for date and time formatting, so the format may vary from platform to platform and from country to country. toLocaleString() returns a string formatted in what is likely the user’s preferred date and time format.

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.)

Server side projection with DateTime property is not deserialized on the client

I have a projection defined on the server (IQuerable - anonymous type). The anonymous type contains DateTime properties, which are not deserialized on the client, but instead remain strings.
How can I easily convert them to JS DateTime objects in the same way as breeze normally does?
I have the default WebApi configuration.
If you need to deserialize them manually, then the best library I've found is moment.js
It should be as simple as: var m = moment("DATE_AS_STRING_HERE"); and you can then get a native JS date by calling m.toDate();
Moment handles a number of string date formats out of the box, and I've found it to work well with WebApi.
Update:
From the Docs:
The following ISO-8601 formats are supported across all browsers.
"YYYY-MM-DD"
"YYYY-MM-DDTHH"
"YYYY-MM-DD HH"
"YYYY-MM-DDTHH:mm"
"YYYY-MM-DD HH:mm"
"YYYY-MM-DDTHH:mm:ss"
"YYYY-MM-DD HH:mm:ss"
"YYYY-MM-DDTHH:mm:ss.SSS"
"YYYY-MM-DD HH:mm:ss.SSS"
"YYYY-MM-DDTHH:mm:ss Z"
"YYYY-MM-DD HH:mm:ss Z"
Note: Automatic cross browser ISO-8601 support was added in version 1.5.0
And:
Moment.js does detect if you are using an ISO-8601 string and will
parse that correctly without a format string.
So it appears that yes, it should parse ISO-8601 strings fine (indeed, this is its preferred string format).
I ended up using breeze.DataType.parseDateFromServer(dateString); - don't know if this is optimal, but it works correctly with the default WebApi configuration.
Moment needs "Z" at the end of the string to parse a date as UTC. Breeze in contrast, treats dates without timezone information as UTC.

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