Get vs Post Auto Mapping dates C# - c#

I am having some confusion figuring out why I am experiencing the following:
If I using jquery AJAX to "post" some Json data containing a date to an MVC controller the automapper maps the date in the format dd/mm/yyyy however if I use "get" instead the automapper seems to convert the date in to the format mm/dd/yyyy.
Does anybody know why this would be the case? When I check the json payload and querystrings for the post and get respectively they are both in the same format. The date I am using goes across as "1/7/2013" in both cases.
Regards,
Gary

Are you using a JSON serializer for the POST but a DateTime.Parse for the GET? This could yield two different results.
User DateTime.ParseExact to ensure consistent results. I.E.
DateTime.ParseExact(input, "dd/MM/yyyy HH:mm", null);

Related

Convert DateTime Format in Web API

We exposed a Web API with OData protocol. The issue we had is that customer sends requests with a defined DateTime format. Something like this: 2020-10-10T10:21+02:00. How we could convert this date to this format 2020-10-10T12:21? Is it possible to be done in WebAPIConfig? or it should be done in controllers part? How it could be reached?
The easiest way is to use the method .ToString() with the correct parameter on the Datetime object in the controller to get the format that u want to use.
Example of datetime formats .ToString()

JSON parse DateTime

From the server I get this string
[{\"id\":\"9b77ff1e-350e-44d8-8860-15e80f4d8a22\",\"code\":\"C\",\"name\":\"0%\",\"validFrom\":\"2013-07-01T00:00:00Z\",\"validTill\":null,\"active\":1,\"vatProc\":0.0,\"createdAt\":\"2015-02-01T13:04:17.733Z\",\"updatedAt\":\"2015-02-01T13:04:17.733Z\"}]
On the client side I then convert it to a json object using JSON.parse which produces the result:
[
{
active:1
code:"C"
createdAt:"2015-02-01T13:04:17.733Z"
id:"9b77ff1e-350e-44d8-8860-15e80f4d8a22"
name:"0%"
updatedAt:"2015-02-01T13:04:17.733Z"
validFrom:"2013-07-01T00:00:00Z"
validTill:null
vatProc:0
}
]
Which is as you would expect if it where not for the date fields.
The dates are on server translated to UTC.
I know I can pass a function to parse the function to do the conversion.
I would just like to format the date's so that the regular parse would do it "right" if you know what I mean
I saw on the net that there is no "right" way and that every case what I was reading about on the net is working with conversion on the client side.
So is there really no way to tell the regular json parser that that is a date?
I have the option to convert that date-time to what ever would work on the server
ClientSide: AngularJS
ServerSide: C# WCF / json
EDIT:
The problem that I am having is that if I use for instance the ValidFrom as a model in AngularJS it is complaining that it is not a date witch it really is not
AngularJS error:
Expected `2013-07-01T00:00:00Z` to be a date
Before I send a date to the client I set the DateTimeKind property to utc
SomeObject.Date= DateTime.SpecifyKind(SomeObject.Date, DateTimeKind.Utc);
Before saving any date to the server I do:
SomeObject.Date= SomeObject.Date.ToUniversalTime();
The only requirement on the client is to use momentjs format() method in order to convert the date to ISO-8601
Following this method JSON always handles times correctly on the client (at least it has worked fine so far for me)
This helped me a lot

Locale Date formatting with MVC, AJAX and Moment.js

I'm probably blind to something right in front of me.
But In my MVC application, I have an object/model that has a DateTime property.
I do an AJAX GET to retrieve the model. The datetime when received looks something like Date(142342323).
I want to convert this date to the locale setting of the user. In moment.js I don't see a way to set it to local.
I thought about getting the currentculture in MVC and passing that as a value (and storing it as an hidden field on the page ) and then using that for the javascript date format...but there seems to be a discrepancy between c# formats and javascript formats.
Ideas anyone?
I mainly have 1 format for Europe and 1 for US (dd/mm/yyyy and mm/dd/yyyy).

Datetime formats and JSON data in C#

I'm having an issue with date/time formats in ASP.NET/C#. I have my SQL Server database set up with a PostDate field set to a type of "datetime". But it's saving the date in a strange format. I added a new row through a form and I got this as the date/time string:
2012-09-28 14:56:48.910
When it gets parsed by JSON.NET it gets even stranger. I get:
2012-09-28T14:56:48.91
The date and time are obviously correct, but how do I set things so that I can parse the date into a human-friendly way? There isn't really any code to post because the date is being added when the row is inserted. I'd like to format this as "Sept. 28, 2012 2:56 pm". How do I do that? Do I need to format the string before or after it's parsed as JSON?
That's not a "strange" format at all. The second form is ISO-8601; the first is ISO-8601 without the T. Considering the strange formats you can get in JSON, it looks like you've been let off pretty lightly!
Serialization formats aren't meant to be user-friendly, particularly - they're meant to be machine-to-machine formats.
I would hope that JSON.NET would give you a DateTime after parsing; it should only be giving you the ISO-8601 format after you've converted back to JSON.
If you've got a DateTime that you want to format for user consumption, there are all kinds of options with standard and custom format strings. Don't forget that you should respect the culture of the user, as far as possible - so make sure you're taking appropriate steps to either set the thread's current culture to be the user's one, or that you're passing the culture explicitly to DateTime.ToString etc.
You can try it in C#:
.ToString("MMM d yyyy, h:mm tt")

datetime.parse and making it work with a specific format

I have a datetime coming back from an XML file in the format:
20080916 11:02
as in
yyyymm hh:ss
How can I get the datetime.parse function to pick up on this? Ie parse it without erroring?
DateTime.ParseExact(input,"yyyyMMdd HH:mm",null);
assuming you meant to say that minutes followed the hours, not seconds - your example is a little confusing.
The ParseExact documentation details other overloads, in case you want to have the parse automatically convert to Universal Time or something like that.
As #Joel Coehoorn mentions, there's also the option of using TryParseExact, which will return a Boolean value indicating success or failure of the operation - I'm still on .Net 1.1, so I often forget this one.
If you need to parse other formats, you can check out the Standard DateTime Format Strings.
Thanks for the tip, i used this to get my date "20071122" parsed, I needed to add datetimestyles, I used none and it worked:
DateTime dt = DateTime.MinValue;
DateTime.TryParseExact("20071122", "yyyyMMdd", null,System.Globalization.DateTimeStyles.None, out dt);

Categories

Resources