1

Unlike the U.S.A ... most other countries uses the dd/MM/yyyy format (from smallest to biggest). However, .NET naturally takes in date in MM/dd/yyyy format.

I have an input that accepts a datetime, and the user will want to type in the date in dd/MM/yyyy format, let's say they type in 30/1/2017 ... but when that date is posted in the backend, it becomes unrecognized.. or it becomes reversed (1/2/2017 becomes 2/1/2017).

[HttpPost]
public ActionResult Save(DateTime date) // user entered 1/2/2017 from front-end
{
    date.ToString("dd/MM/yyyy"); // this becomes 2/1/2017
}

Is there some kind of global setting to reverse this recognization of date in .NET? I would not like to manually switch dates from front-end because that seems like alot of work and alot of places to do it from.

andyh0316
  • 355
  • 2
  • 17
  • 1
    The problem is not in this method. You receive a DateTime value. A DateTime has no format. It is how do you create the input field and how do you set the formatting attributes that decides how the Binder transform the inputted text to a datetime. If there is an error is there not here. Please add your ViewModel (if any) and the markup in the view page where the input text that get this value is defined. – Steve Apr 08 '17 at 13:39
  • Also look at this question http://stackoverflow.com/questions/11272851/format-datetime-in-asp-net-mvc-4 – Steve Apr 08 '17 at 13:40
  • *"However, .NET naturally takes in date in MM/dd/yyyy format."* Actually .NET takes in the format for whatever the culture the current thread is running under is by default. So if the culture is set to a country that does `dd/MM/yyyy` then that is the way it will be parsed. – Scott Chamberlain Apr 08 '17 at 19:34
  • @ScottChamberlain thanks.. this is the answer im looking for .. ill look more into how to do that. – andyh0316 Apr 09 '17 at 10:20
  • @andyh0316 that is what Clay is saying [in his answer too](http://stackoverflow.com/a/43298095/80274) – Scott Chamberlain Apr 09 '17 at 15:21

4 Answers4

2

You'd be better off setting the culture on the thread or in the controller initialization, or in the routing. There are a couple of answers in this question that show several excellent ways to do it.

The point is, the Thread.CurrentCulture controls the formatting of date/time and currency, among other cool things...so you can focus on the real solution, and leave all the trivial work to the framework.

Community
  • 1
  • 1
Clay
  • 4,999
  • 1
  • 28
  • 45
0

DateTime.ParseExact is one of the shots you can try.

You need to specify culture, and then parse the date:

CultureInfo provider = CultureInfo.InvariantCulture;
DateTime.ParseExact("01/02/2017", "dd/MM/yyyy", provider);
DateTime.ParseExact("01/02/2017", "MM/dd/yyyy", provider);

If you will show output, the first one will be [01.02.2017 00:00:00] and the seccond [02.01.2017 00:00:00]

Your solution does not work, because when you are getting the data, it's probalbly get with the same American format of "MM/dd/yyyy"

Community
  • 1
  • 1
titol
  • 999
  • 13
  • 25
0

DateTime isn't a good idea for serialisation/deserialisation like that because of stuff like this, or timezones. Rather than trying to hack it such that it works, avoid it altogether by sending it as a Unix timestamp (or similar things, like strings with timezone info that you then parse), and then turn it into a DateTime on your end.

You can follow this answer and cater it to your purposes like so:

[HttpPost]
public ActionResult Save(double timestamp)
{
    var date = new DateTime(1970,1,1,0,0,0,0,System.DateTimeKind.Utc).AddSeconds( timestamp );
}

This isn't ideal, but there must be a way to automate it. Be aware your front-end will need to send it as a normal Unix timestamp (Javascript, for example, uses a timestamp in milliseconds instead of seconds.) and as UTC. This also avoids timezone issues.

Mike Roibu
  • 307
  • 2
  • 13
0

If you will going to use this format conversion multiple times, why not to use a Helper Method ?

Like this c# console aplication exemple:

static void Main(string[] args)
{
    DateTime mydate = new DateTime(2017, 04, 08);
    string myValue = convertDate(mydate);

    Console.WriteLine(myValue);

    Console.ReadKey();
}

private static string convertDate(DateTime dateToConvert)
{
    return string.Format("{0:MM/dd/yyyy}", dateToConvert);
}
Luís Chaves
  • 661
  • 7
  • 15