How to Convert Date Format to Dd-Mm-Yyyy in C#

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 Convert from MMM-yyyy to dd-MM-yyyy or yyyy-MM-dd

Use

DateTime.ParseExact(dt, "MMM-yyyy", CultureInfo.InvariantCulture)

This will give you first of the month anyway.

how to change date format to mm/dd/yyyy to dd/mm/yyyy in c#

Or Try to parse the datetime.
For example

DateTime dateformat = DateTime.ParseExact(Convert.ToDateTime(bin.timestamp), "dd/M/yyyy h:mm:ss tt",CultureInfo.InvariantCulture);
binModel.UpdatedTime = TimeZoneInfo.ConvertTimeFromUtc(dateformat,TimeZoneInfo.FindSystemTimeZoneById("India Standard Time")).ToString();

how to convert mm/dd/yyyy format to dd/mm/yyyy in c#

You need to change the string format slightly as the string you're using is not valid. Change hh to h:

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

If you're passing a string that can have single digit for month or day then you should use the following:

"M/d/yyyy h:mm:ss tt"

If it always has 2 digit for month and date then you can use:

"MM/dd/yyyy h:mm:ss tt"

hh means use 2 digits for the hour, h means use one (where possible). You're also setting tt which displays AM/PM which could be causing the issue when used with hh.

EDIT: (From your comments) If updatedDate is a DateTime object then you can just do:

var time = updatedDate.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);

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

How to convert any date format to yyyy-MM-dd

string DateString = "11/12/2009";
IFormatProvider culture = new CultureInfo("en-US", true);
DateTime dateVal = DateTime.ParseExact(DateString, "yyyy-MM-dd", culture);

These Links might also Help you

DateTime.ToString() Patterns

String Format for DateTime [C#]



Related Topics



Leave a reply



Submit