Why Does Datetime.Tostring("Dd/Mm/Yyyy") Give Me Dd-Mm-Yyyy

why does DateTime.ToString(dd/MM/yyyy) give me dd-MM-yyyy?

Slash is a date delimiter, so that will use the current culture date delimiter.

If you want to hard-code it to always use slash, you can do something like this:

DateTime.ToString("dd'/'MM'/'yyyy")

DateTime ToString(“dd/MM/yyyy”) returns dd.MM.yyyy

The / character in date/time format strings stands for "whatever the date separator of the format provider is". Since you do not supply a format provider Thread.CurrentCulture is used, and in your case the current culture uses . as the date separator.

If you want to use a literal slash, place it inside single quotes:

dateTime.ToString("dd'/'MM'/'yyyy");

Alternatively, you could specify a format provider where the date separator is /:

dateTime.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);

All of the above is documented on MSDN.

See the difference in a live example.

convert datetime to date format dd/mm/yyyy

DateTime dt = DateTime.ParseExact(yourObject.ToString(), "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);

string s = dt.ToString("dd/M/yyyy", CultureInfo.InvariantCulture);

DateTime.Today.ToString(dd/mm/yyyy) returns invalid DateTime Value

Lower mm means minutes, so

DateTime.Now.ToString("dd/MM/yyyy");  

or

DateTime.Now.ToString("d");  

or

DateTime.Now.ToShortDateString()

works.

Standard Date and Time Format Strings

How to convert DateTime in dd/MM/yyyy to DateTime in dd-MM-yyyy?

First of all - DateTime has no some formats. string that represents DateTime can have formats.

To convert DateTime to specific format to string you can use ToString()
method:

DateTime dt = DateTime.Now;
string date = dt.ToString("dd-MM-yyyy");

To parse string to DateTime you can use ParseExact() method:

string date = "02/03/2017";
DateTime dt = DateTime.ParseExact(date, "dd/MM/yyyy", CultureInfo.InvariantCulture);

or

string date = "02-03-2017";
DateTime dt = DateTime.ParseExact(date, "dd-MM-yyyy", CultureInfo.InvariantCulture);

FOR YOUR EDIT:

Convert.ToDateTime() without CultureInfo tries to convert string to DateTime using your PC culture. If you want to use Convert.ToDateTime() use overloaded method that accept string and culture:

DateTime dt = Convert.ToDateTime(someDate, CultureInfo.InvariantCulture);

DateTime.ToString(MM/dd/yyyy HH:mm:ss.fff) resulted in something like 09/14/2013 07.20.31.371

Is it because some culture format issue?

Yes. Your user must be in a culture where the time separator is a dot. Both ":" and "/" are interpreted in a culture-sensitive way in custom date and time formats.

How can I make sure the result string is delimited by colon instead of dot?

I'd suggest specifying CultureInfo.InvariantCulture:

string text = dateTime.ToString("MM/dd/yyyy HH:mm:ss.fff",
CultureInfo.InvariantCulture);

Alternatively, you could just quote the time and date separators:

string text = dateTime.ToString("MM'/'dd'/'yyyy HH':'mm':'ss.fff");

... but that will give you "interesting" results that you probably don't expect if you get users running in a culture where the default calendar system isn't the Gregorian calendar. For example, take the following code:

using System;
using System.Globalization;
using System.Threading;

class Test
{
static void Main()
{
DateTime now = DateTime.Now;
CultureInfo culture = new CultureInfo("ar-SA"); // Saudi Arabia
Thread.CurrentThread.CurrentCulture = culture;
Console.WriteLine(now.ToString("yyyy-MM-ddTHH:mm:ss.fff"));
}
}

That produces output (on September 18th 2013) of:

11/12/1434 15:04:31.750

My guess is that your web service would be surprised by that!

I'd actually suggest not only using the invariant culture, but also changing to an ISO-8601 date format:

string text = dateTime.ToString("yyyy-MM-ddTHH:mm:ss.fff", CultureInfo.InvariantCulture);

This is a more globally-accepted format - it's also sortable, and makes the month and day order obvious. (Whereas 06/07/2013 could be interpreted as June 7th or July 6th depending on the reader's culture.)

Converting dd/mm/yyyy formatted string to Datetime

You need to use DateTime.ParseExact with format "dd/MM/yyyy"

DateTime dt=DateTime.ParseExact("24/01/2013", "dd/MM/yyyy", CultureInfo.InvariantCulture);

Its safer if you use d/M/yyyy for the format, since that will handle both single digit and double digits day/month. But that really depends if you are expecting single/double digit values.


Your date format day/Month/Year might be an acceptable date format for some cultures. For example for Canadian Culture en-CA DateTime.Parse would work like:

DateTime dt = DateTime.Parse("24/01/2013", new CultureInfo("en-CA"));

Or

System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-CA");
DateTime dt = DateTime.Parse("24/01/2013"); //uses the current Thread's culture

Both the above lines would work because the the string's format is acceptable for en-CA culture. Since you are not supplying any culture to your DateTime.Parse call, your current culture is used for parsing which doesn't support the date format. Read more about it at DateTime.Parse.


Another method for parsing is using DateTime.TryParseExact

DateTime dt;
if (DateTime.TryParseExact("24/01/2013",
"d/M/yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out dt))
{
//valid date
}
else
{
//invalid date
}

The TryParse group of methods in .Net framework doesn't throw exception on invalid values, instead they return a bool value indicating success or failure in parsing.

Notice that I have used single d and M for day and month respectively. Single d and M works for both single/double digits day and month. So for the format d/M/yyyy valid values could be:

  • "24/01/2013"
  • "24/1/2013"
  • "4/12/2013" //4 December 2013
  • "04/12/2013"

For further reading you should see: Custom Date and Time Format Strings

DateTime.Now.ToString(dd/MM/yyyy HH:mm:ss) does not account my format

Use this:

DateTime.Now.AddMinutes(55).ToString("dd'/'MM'/'yyyy HH:mm:ss")

because / means default date separator, so it's associated with your current culture. So know it will use always / here, no matter of current culture.

Read more here at MSDN

C# DateTime to YYYYMMDDHHMMSS format

DateTime.Now.ToString("yyyyMMddHHmmss"); // case sensitive

DateTime.ToString from all formats to dd/MM/YYYY?

Actually, you don't need all these lines of code. You just need this:

 // We just have to pass to the ToString
// method the exact format we want. Under the hood the CLR has
// the know how to execute this command and you get the desired
// output.
string DateString = DateTime.Now.ToString("dd-MMM-yy");

Furthermore, we use the DateTime.ParseExact method, when we want to get this exception you have mentioned. Saying this, I mean that we know that the string representation of dates, which we want to parse are of the exact format, we have specified in DateTime.ParseExact and if some of them aren't we wan't to be informed know it. Usually, we would have a try catch clause and in the catch clause we log this.



Related Topics



Leave a reply



Submit