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.
Related
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.
I am consuming a WCF Service in Javascript and need to play around with dates.
I was looking around for some nice DateTime handlers for the format the DataContractJsonSerializer generates { "date": "/Date(1260597600000-0600)/" } and found moment.js. moment.js is really excellent to consume this date format, handles the format including the timezone.
What I need now is generate the WCF date format from a Javascript or moment Date to send dates with timezones in the request on my POST method and looking in the documentation of moment.js couldn't find anything that has the output I need.
Any idea how to achieve this with moment.js or any other js library?
Thanks.
With moment.js:
yourMomentObject.format("/[Date](xZZ)/")
Example:
Without moment, you can write your own function that takes uses the Date object's .getTime() function, and .getTimezoneOffset() functions. However, the offset has to be negated, then formatted properly before being added to the string.
How do I return yesterday day in the format of yyyy-mm-dd using JavaScript and using
the regular expression below.
((19|20)\d\d)-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])
Native JS date formatting is very primitive (see this question for more details).
If you can, a better solution is to use Moment.js to make working with dates in JS much, much better.
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.
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/