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

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.

Related

Generate WCF JSON format Date with moment.js or another javascript library

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.

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.

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/

javascript client date format

I had a problem on extjs date. Seem by default it using computer date to create a date.
I don't check up my pc and post the date.Then i found it based on American
M/d/yyyy.
After i change the system regional setting date to d-m-Y.Everthing work fine.
So anybody know how extjs get client date format ?
e.g 'YYYY-mm-dd' or 'M/d/yyyy' .
Since i need to parse the format from extjs code to php to mysql date format.
I try to find stackoverflow and site but seem not found out,
You can convert date to String when transfer it so that you can set the date format by yourself.
Actually the default format used is 'm/d/y', and you can change the format self, and I don't think so the ext can get the client date format and change the format consistently.
dont mind your regional setting
you have two options
in your server you convert to string and change format
yourDate.ToString("MMM dd, yyyy")
see here or here for standard and custom format
or in your extjs code you use
renderer: Ext.util.Format.dateRenderer('d-M-Y'),
see here for available format strings.

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