Reputation: 1425
I want to format the input string into MM/dd/yyyy hh:mm:ss format in C#.
The input string is in format MM/dd/yyyy hh:mm:ss
For example :"04/30/2013 23:00"
I tried Convert.ToDateTime()
function, but it considers 4 as date and 3 as month which is not what I want. Actually month is 04 and date is 03.
I tried DateTime.ParseExact()
function also, But getting Exception.
I am getting error:
String was not recognized as a valid DateTime.
Upvotes: 12
Views: 173293
Reputation: 310
I have created an extension method that I use everytime
public static DateTime? ToDateTime(this string dateTime)
{
if (string.IsNullOrEmpty(dateTime))
{
return null;
}
return DateTimeOffset.ParseExact(dateTime, new string[] { "MMM dd, yyyy", "dd/MM/yyyy", "d/M/yyyy", "dd-MM-yyyy", "d-M-yyyy", "yyyy-MM-ddTHH:mm:ssZ", "yyyy-MM-dd" }, CultureInfo.InvariantCulture).DateTime;
}
I have added multiple formats that I encounter in an array and I reuse this method every time, the above method works for the following examples of date:
additionally in c# the answer of @Manjay can also be tried as follows :
char formatChar = '-';
if (dateTime.Contains('-'))
{
formatChar = '-';
}
if (dateTime.Contains('/'))
{
formatChar = '/';
}
string[] parts = dateTime.Split(formatChar);
var month = parts[0].PadLeft(2, '0');
var day = parts[1].PadLeft(2, '0');
var year = parts[2];
string properFormattedDate = $"{month}{formatChar}{day}{formatChar}{year}";
return DateTimeOffset.ParseExact(properFormattedDate, new string[] { "MMM dd, yyyy", "dd/MM/yyyy", "dd-MM-yyyy", "yyyy-MM-ddTHH:mm:ssZ", "yyyy-MM-dd" }, CultureInfo.InvariantCulture).DateTime;
Upvotes: 0
Reputation: 301
You may use this type format (get formatted data from sql server)
FORMAT(convert(datetime,'16/04/2018 10:52:20',103),'dd/MM/yyyy HH:mm:ss', 'en-us')
CONVERT(VARCHAR,convert(datetime,'16/04/2018 10:52:20',103), 120)
Upvotes: 0
Reputation: 2245
This can also be the problem if your string is 6/15/2019. DateTime Parse expects it to be 06/15/2019.
So first split it by slash
var dateParts = "6/15/2019"
var month = dateParts[0].PadLeft(2, '0');
var day = dateParts[1].PadLeft(2, '0');
var year = dateParts[2]
var properFormat = month + "/" +day +"/" + year;
Now you can use DateTime.Parse(properFormat, "MM/dd/yyyy"). It is very strange but this is only thing working for me.
Upvotes: 2
Reputation: 4316
Below code worked for me:
string _stDate = Convert.ToDateTime(DateTime.Today.AddMonths(-12)).ToString("MM/dd/yyyy");
String format ="MM/dd/yyyy";
IFormatProvider culture = new System.Globalization.CultureInfo("fr-FR", true);
DateTime _Startdate = DateTime.ParseExact(_stDate, format, culture);
Upvotes: 0
Reputation: 109
DateTime dt1 = DateTime.ParseExact([YourDate], "dd-MM-yyyy HH:mm:ss",
CultureInfo.InvariantCulture);
Note the use of HH (24-hour clock) rather than hh (12-hour clock), and the use of InvariantCulture because some cultures use separators other than slash.
For example, if the culture is de-DE, the format "dd/MM/yyyy" would expect period as a separator (31.01.2011).
Upvotes: 1
Reputation: 98740
You can use DateTime.ParseExact()
method.
Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.
DateTime date = DateTime.ParseExact("04/30/2013 23:00",
"MM/dd/yyyy HH:mm",
CultureInfo.InvariantCulture);
Here is a DEMO
.
hh
is for 12-hour clock from 01 to 12, HH
is for 24-hour clock from 00 to 23.
For more information, check Custom Date and Time Format Strings
Upvotes: 5
Reputation: 9862
try this:
string strTime = "04/30/2013 23:00";
DateTime dtTime;
if(DateTime.TryParseExact(strTime, "MM/dd/yyyy HH:mm",
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None, out dtTime))
{
Console.WriteLine(dtTime);
}
Upvotes: 3
Reputation: 39600
Your date time string doesn't contains any seconds. You need to reflect that in your format (remove the :ss
).
Also, you need to specify H
instead of h
if you are using 24 hour times:
DateTime.ParseExact("04/30/2013 23:00", "MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture)
See here for more information:
Custom Date and Time Format Strings
Upvotes: 16
Reputation: 176886
change the culture and try out like this might work for you
string[] formats= { "MM/dd/yyyy HH:mm" }
var dateTime = DateTime.ParseExact("04/30/2013 23:00",
formats, new CultureInfo("en-US"), DateTimeStyles.None);
Check for details : DateTime.ParseExact Method (String, String[], IFormatProvider, DateTimeStyles)
Upvotes: 1