Converting string to date time - c#

I have a date which comes in a string like so:
09/25/2014 09:18:24
I need it like this (yyyy-mm-dd):
2014-09-25 09:18:24
The object that this date goes into is a nullable date.
Tried this does not work:
DateTime formattedDate;
bool result = DateTime.TryParseExact(modifiedDate, "yyyy-MM-dd",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out formattedDate);
Any clues?
Thanks in advance.

From DateTime.TryParseExact
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.
In your case, they are not. Use yyyy-MM-dd HH:mm:ss format instead.
string s = "2014-09-25 09:18:24";
DateTime dt;
if(DateTime.TryParseExact(s, "yyyy-MM-dd HH:mm:ss",
CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
Console.WriteLine(dt);
}
It is a little bit unclear but if your string is 09/25/2014 09:18:24, then you can use MM/dd/yyyy HH:mm:ss format instead. Just a tip, "/" custom format specifier has a special meaning as replace me with current culture or supplied culture date separator. That means, if your CurrentCulture or supplied culture's DateSeparator is not /, your parsing operation will fail even if your format and string matches exactly.
If you have already a DateTime and you want to format it, you can use DateTime.ToString(string) method like;
dt.ToString("yyyy-mm-dd", CultureInfo.InvariantCulture); // 2014-09-25
or
dt.ToString("yyyy-mm-dd HH:mm:ss", CultureInfo.InvariantCulture); // 2014-09-25 09:18:24
Remember, a DateTime does not have any implicit format. It just contains date and time values. String representations of them have formats.

In answer to your question, to convert it as you prefer, do it like this:
string originalDate = "09/25/2014 09:18:24";
DateTime formattedDate;
if (DateTime.TryParseExact(originalDate, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out formattedDate))
{
string output = formattedDate.ToString("yyyy-mm-dd HH:mm:ss", CultureInfo.InvariantCulture);
}
And then output will have your desired format.

DateTime dateOf = Convert.ToDateTime("09/25/2014 09:18:24");
string myFormat = "yyyy-mm-dd";
string myDate = dateOf.ToString(myFormat); // output 2014-18-25
Datetime format

Related

Format datetime from string "20151210T11:25:11123"

I have string format "20151210T11:25:11123", can't convert to type DateTime in C# help me?
string date = "20151210T11:25:11123";
DateTime datea = DateTime.ParseExact(date, "dd/MM/yyyy hh:mm tt", CultureInfo.InvariantCulture);
You are using a time of 20151210T11:25:11123 but telling it to parse it as if it were formatted as dd/MM/yyyy hh:mm tt. The format does not match the string, so you get a FormatException. You need to provide a format that matches the string you have. It isn't clear to me what the last 5 digits are but a format like yyyyMMddThh:mm:ssfff will parse the string as 12/10/2015 11:25:11 AM. You may need to adjust the last part of the format to match whatever is actually encoded there in your string.
string date = "20151210T11:25:11123";
DateTime datea = DateTime.ParseExact(date, "yyyyMMddThh:mm:ssfff", CultureInfo.InvariantCulture)
Console.WriteLine(datea); // 12/10/2015 11:25:11 AM
with value datetime string ="20160121T13:26:24090"
code error :
DateTime datea = DateTime.ParseExact(date, "yyyyMMddThh:mm:ssfff", CultureInfo.InvariantCulture)
Console.WriteLine(datea); // 12/10/2015 11:25:11 AM

How to convert date format to custome date format?

I have the following output in string:
24/05/15 11:40:50 AM
now I want to convert this string into -> 2015-05-24 11:40:50.000
I have tried below method but it gives me error :
String was not recognized as a valid DateTime.
DateTime.ParseExact("24/05/15 11:40:50 AM",
"yyyy-MM-dd HH:mm:ss",
CultureInfo.InvariantCulture);
From documentation;
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.
In your case, they are not.
First, you can parse it to DateTime with specific format and you can generate a string representation with a specific format from that DateTime. Like;
string s = "24/05/15 11:40:50 AM";
DateTime dt;
if(DateTime.TryParseExact(s, "dd/MM/yy hh:mm:ss tt", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
Console.WriteLine(dt.ToString("yyyy-MM-dd hh:mm:ss.fff"));
}
Prints;
2015-05-24 11:40:50.000

Convert a string to DateTime in C#

I am trying to convert a string to a DateTime for some hours now,
The string looks like this
"20140519-140324" and I know its in UTC
I've allready tried this
DateTime ourDateTime;
bool success = DateTime.TryParseExact(Date, "yyyy-MM-dd-HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out ourDateTime);
StartTime.Text = ourDateTime.ToString("g");
and this
DateTime ourDateTime= DateTime.ParseExact(Date, "yyyy-MM-dd-HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
StartTime.Text = ourDateTime.ToString("g");
but none of these work. What I am not doing properly?
From DateTime.TryParseExact method
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.
In your example, they are not. Use yyyyMMdd-HHmmss custom format instead which exactly matches with your string.
Here an example on LINQPad;
string s = "20140519-140324";
DateTime dt;
if(DateTime.TryParseExact(s, "yyyyMMdd-HHmmss", CultureInfo.InvariantCulture,
DateTimeStyles.AdjustToUniversal, out dt))
{
dt.Dump();
}
Here a demonstration.
Your DateTime.ParseExact example also won't work because of the same reason.
For more information;
Custom Date and Time Format Strings
You are using the wrong format in the TryParseExact method.
the format parameter should be an indicator to the format of the input string.
therefor you need to do this:
DateTime ourDateTime;
bool success = DateTime.TryParseExact(Date, "yyyyMMdd-HHmmss", System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out ourDateTime);
if(success) {
StartTime.Text = ourDateTime.ToString("g");
}

Convert String to DateTime with pattern "MM/dd/yyyy hh:mm:ss"

I have a string variable containing "02/27/2014 23:00:28"
When I use this following code to convert it to Datetime type, the conversion fails (test returns false and parsedDate contains "01/01/0001 00:00:00")
code:
string date = "02/27/2014 23:00:28"
string pattern = "MM/dd/yyyy hh:mm:ss";
DateTime parsedDate;
bool parsedSuccessfully = DateTime.TryParseExact(date, pattern, null, DateTimeStyles.None, out parsedDate);
Thank you!
You need to use uppercase HH for the hours since you are using 24h format.
MM/dd/yyyy HH:mm:ss
You also need to use CultureInfo.InvariantCulture instead of null to ensure that / will be used as date separator. Otherwise it is replaced by your cultures actual date separator. *
bool test = DateTime.TryParseExact(date, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedDate);
The "/" Custom Format Specifier *
hh specifier is for 01 to 12.
Use HH specifier which is for 00 to 23. (24-hour clock based)
And I think you should use date instead of test in your DateTime.TryParseExact method.
string date = "02/27/2014 23:00:28";
string pattern = "MM/dd/yyyy HH:mm:ss";
DateTime parsedDate;
bool test= DateTime.TryParseExact(date, pattern,
CultureInfo.InvariantCulture,
DateTimeStyles.None, out parsedDate);
Console.WriteLine(test); // True
Since you using null in your IFormatProvider parameter, it uses CurrentCulture. From documentation;
If provider is null, the CultureInfo object that corresponds to the
current culture is used.
But / format specifier has a special meaning of "replace me with the current culture date seperator" in your string format.
That means, if your current culture's date separator is not /, your parsing operation will be fail. That's why you should use InvariantCulture in such a case.
Here an another answer: TryParseExact returns false, though I don't know why
24-hour time format uses HH. So your format should be
"MM/dd/yyyy HH:mm:ss"

C# DateTime Conversion from yyyy-MM-ddTHH:mm:ss to dd MMM yyyy

How to convert "yyyy-MM-ddTHH:mm:ss" into "dd MMM yyyy" format? For Instance, i want to convert 2013-04-16 05:30:05 into 16 April 2013. What is the correct method to achieve this?
First ParseExact then do ToString (I assume that you have string object, if you have DateTime object, skip first line)
var dateTime = DateTime.ParseExact(yourDateString, "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture);
var yourNewString = dateTime.ToString("dd MMM yyyy");
Note that representation of DateTime you see in debugger is dependant on your current culture.
First, a DateTime has no format. But if you've already a string that represents a DateTime with the format yyyy-MM-ddTHH:mm:ss and you want to convert it to a string-date with format dd MMM yyyy you need to parse it to DateTime first.
Therefore use DateTime.ParseExact:
DateTime dt = DateTime.ParseExact("2013-04-16 05:30:05", "yyyy-MM-dd HH:mm:ss", null);
Now you can use DateTime.ToString:
string result = dt.ToString("dd MMM yyyy");
Note that you need to pass a different CultureInfo object to ParseExact/ToString if you want to parse with another DateTimeFormat than your current (f.e. force english month names instead of german: dt.ToString("dd MMM yyyy", CultureInfo.InvariantCulture)).
As others mentioned, a DateTime has no format. To parse a string literal to a Date you need to call DateTime.Parse (if the string is in a culture-specific format) or DateTime.ParseExact if you need to pass a format string.
The format can be a custom format like yyyy-MM-dd HH:mm:ss or one of the standard format strings, eg. s for yyyy-MM-ddTHH:mm:ss.
2013-04-16 05:30:05 it not in one of the standard formats, so you have to parse by passing a custom format string:
var dt = DateTime.ParseExact("2013-04-16 05:30:05", "yyyy-MM-dd HH:mm:ss", null);
On the other hand, yyyy-MM-ddTHH:mm:ss is the s standard format so you can just write:
var dt = DateTime.ParseExact("2013-04-16T05:30:05", "s", null);

Categories

Resources