I want to convert this string "12092014" into DateTime object as 12 september 2014.
If ddMMyyyy is a standard date and time format of your CurrentCulture, you can use DateTime.Parse directly;
var date = DateTime.Parse("12092014");
If it is not, you can use custom date and time format with DateTime.TryParseExact method like;
string s = "12092014";
DateTime dt;
if(DateTime.TryParseExact(s, "ddMMyyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
Console.WriteLine(dt);
}
Related
Tried the below but it only takes care of one format
string date = "20100102";
DateTime datetime = DateTime.ParseExact(date, "yyyyMMdd", CultureInfo.InvariantCulture);
Instead of
DateTime datetime = DateTime.ParseExact(date, "yyyyMMdd",
CultureInfo.InvariantCulture);
...try:
var dateString = "20100102";
var formats = new String[]{"yyyyMMdd",
"ddMMyyyy"};
DateTime dateValue;
if (DateTime.TryParseExact(dateString, formats,
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out dateValue))
Console.WriteLine ("Success");
MSDN has this to say on DateTime.TryParseExact:
Converts the specified string representation of a date and time to its DateTime equivalent using the specified array of formats, culture-specific format information, and style. The format of the string representation must match at least one of the specified formats exactly. The method returns a value that indicates whether the conversion succeeded.
Tell me more
DateTime.TryParseExact
I have a string like 2015-07-30T11:11:00+0200. How can I parse it to DateTime object? DateTime.Parse(string) throws null exception, ParseExact too.
I would parse it to DateTimeOffset instead of DateTime since your string has UTC offset part.
string s = "2015-07-30T11:11:00+0200";
DateTimeOffset dt;
if(DateTimeOffset.TryParseExact(s, "yyyy-MM-dd'T'HH:mm:ssK", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
//
}
Now, you have a DateTimeOffset as
30.07.2015 11:11:00 +02:00
If you wanna get it's DateTime part, you can use it's DateTimeOffset.DateTime property which returns;
30.07.2015 11:11:00
I am trying to get this date string 09 Apr 2015: 15:16:17 to display in this format 09/04/2015 15:16:17. This is what I have tried.
DateTime dtDateTime = new DateTime();
string dateString = "09 Apr 2015: 15:16:17";
DateTime dateValue;
DateTime.TryParse(dateString, out dateValue);
dtDateTime = dateValue;
This is the output 01/01/0001 00:00:00
I thought the TryParse would convert the dateString value to the required DateTime format. What am I doing wrong?
You should go with this:
DateTime dtDateTime = new DateTime();
string dateString = "09 Apr 2015: 15:16:17";
DateTime dateValue;
if (DateTime.TryParseExact(dateString, #"dd MMM yyyy':' HH':'mm':'ss",
new CultureInfo("en-us"), DateTimeStyles.None, out dateValue))
dtDateTime = dateValue;
Using TryParseExact you can provide a custom date format string to match your input date. In the example above I added that extra : after the year.
Also, you must use a CultureInfo which can understand your month name; here I assumed you got an english formatted date.
You need to specify the format since it's not a standard date format string:
DateTime.TryParseExact(
dateString,
"dd MMM yyyy: HH:mm:ss",
CultureInfo.CurrentCulture,
DateTimeStyles.None,
out dateValue);
Also, you should check the result of the call since TryParse and TryParseExact return true/false
You can use TryParseExact method:
DateTime.TryParseExact(dateString, "dd MMM yyyy: HH:mm:ss",
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.AllowWhiteSpaces,
out dtDateTime);
Tips:
If you use MMM, the month will be treated as if it is in 3 letter format (like Apr )
If you use HH rather than hh it means the hour part is in 24-hour format, the it will not fail on parsing 15 as hour
I am getting this date time string from Sharepoint:
2013-01-01 08:00:00:000
I want to convert it to
2013-01-01 08:00:00 AM
Any ideas how to do that? Every time I try to use Date.Parse, I get an error saying the string is not a valid date time type
Best option is parsing it to DateTime and get it's string representation with a specific format.
string s = "2013-01-01 08:00:00:000";
DateTime dt;
if(DateTime.TryParseExact(s, "yyyy-MM-dd HH:mm:ss:fff", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
dt.ToString("yyyy-MM-dd HH:mm:ss tt", CultureInfo.InvariantCulture).Dump();
// 2013-01-01 08:00:00 AM
}
Based on Jon's comment, you can use ParseExact as well.
DateTime dt = DateTime.ParseExact("2013-01-01 08:00:00:000",
"yyyy-MM-dd HH:mm:ss:fff",
CultureInfo.InvariantCulture);
string s = dt.ToString("yyyy-MM-dd HH:mm:ss tt", CultureInfo.InvariantCulture);
Or you can format directly that ParseExact return value;
string s = DateTime.ParseExact("2013-01-01 08:00:00:000",
"yyyy-MM-dd HH:mm:ss:fff",
CultureInfo.InvariantCulture)
.ToString("yyyy-MM-dd HH:mm:ss tt", CultureInfo.InvariantCulture);
I have a custom date format that I want to convert to Datetime so I can then insert into my database, I tried using Datetime.ParseExact() But I think I'm misunderstanding something as the code throws a System.FormatException.
I have the following date format from a csv
> 6/11/2014 9:00
and I wish to convert it to the mysql datetime format
> 0000-00-00 00:00:00 OR yyyy-MM-dd HH:mm:ss
Notice they haven't included the seconds in the original date so I am unsure (without appending them to the end) how to set all records to just have "00" for seconds as it is not available.
I tried the following which throws an exception
DateTime myDate = DateTime.ParseExact("6/11/2014 9:00", "yyyy-MM-dd HH:mm",
System.Globalization.CultureInfo.InvariantCulture);
first thing you need to convert string to date time and than convert datetime tos tring
string strd = "6/11/2014 9:00";
DateTime dt ;
//convert datetime string to datetime
if(DateTime.TryParse(strd, out dt))
{
//convert datetime to custom datetime format
Console.WriteLine("The current date and time: {0: yyyy-MM-dd HH:mm:ss}",
dt); ;
}
output
I know this is late to answer that but I'm really surprised none of answer consider to use IFormatProvider to prevent a possible parsing error because of / format specifier or considering your string is a standard date and time format for your CurrentCulture or not so you can or can't use DateTime.TryParse(string, out DateTime) overload directly.
First of all, let's look at what DateTime.ParseExact documentation says:
Converts the specified string representation of a date and time to its
DateTime equivalent. The format of the string representation must
match a specified format exactly or an exception is thrown.
In your case, they don't match. You should use d/MM/yyyy H:mm format to parse your example string with a culture that have / as a DateSeparator. I almost always suggest to use DateTime.TryParseExact method in this kind of situations;
string s = "6/11/2014 9:00";
DateTime dt;
if(DateTime.TryParseExact(s, "d/MM/yyyy H:mm", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss"));
// result will be 2014-11-06 09:00:00
}
If you know formats of your dates, then you can do this:
string stringDate = "6/11/2014 9:00";
//Your date formats of input
string[] dateFormats = new string[]
{
"d/MM/yyyy H:mm",
"dd/MM/yyyy H:mm",
"dd/MM/yyyy HH:mm",
"dd/MM/yyyy H:mm:ss",
"dd/MM/yyyy HH:mm:ss"
/* And other formats */
};
DateTime convertedDate;
bool isSuccessful = DateTime.TryParseExact(stringDate, dateFormats,
System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out convertedDate);
if (isSuccessful)
{
//If conversion was successful then you can print your date at any format you like
//because you have your date as DateTime object
Console.WriteLine(convertedDate.ToString("dd-MM-yyyy HH:mm:ss")); /* Or other format you want to print */
}
I hope it will be helpful to you.