Convert.ToDateTime('Datestring') to required dd-MM-yyyy format of date - c#

I have string of Date which can be in any format of date but I wanted to convert it to dd-MM-yyyy format.
I have tried every Convert.ToDatetime option which converts only to the System format. I want it to convert dd-MM-yyyy format.
Please reply. Thanks in Advance.

Try this
DateTime dateTime = new DateTime();
dateTime = Convert.ToDateTime(DateTime.ParseExact("YouDateString", "dd-MM-yyyy", CultureInfo.InvariantCulture));
This will return DateTime with your Format

Despite there being many answers and this being a duplicate answer, here are some possible solutions:
string formatted = date.ToString("dd-MM-yyyy");
or
string formatted = date.ToString("dd MM yyyy");
This link might help you with more formats and options.

DateTime.ParseExact has an overloaded method to accepts multiple formats, include (all)possible formats you receive and parse the string. Once you get the valid DateTime you could apply desired format in converting to string.
// ex...
string dateString = ...; // your date.
string[] formats= {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt",
"MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss",
"M/d/yyyy hh:mm tt", "M/d/yyyy hh tt",
"M/d/yyyy h:mm", "M/d/yyyy h:mm",
"MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm",
"MM/d/yyyy HH:mm:ss.ffffff" };
var date = DateTime.ParseExact(dateString, formats, new CultureInfo("en-US"), DateTimeStyles.None);
//convert to desired format.
var strDate = date.ToString("dd-MM-yyyy");

Here we go:
DateTime time = DateTime.Now;
Console.WriteLine(time.Day + "/" + time.Month + "/" + time.Year);
//return: 22/05/2016

Related

Why DateTime TryParse is returning date as {01/01/0001 00:00:00}? [duplicate]

This question already has answers here:
How do I format a DateTime in a different format?
(2 answers)
Closed 3 years ago.
I am doing this
string[] formats = { "yyyy-MM-dd" };
DateTime outDate;
DateTime.TryParseExact(DateTime.Now.ToString(),
formats,
new CultureInfo("en-US"),
DateTimeStyles.None,
out outDate);
interfaceoperation.LogDate = outDate;
interfaceoperation.LogTime = outDate;
LogDate and LogTime are of type DateTime.
But it returns outdate value as {01/01/0001 00:00:00}.
Why?
I think the problem is in the format of the dates, we will have to add the correct format of your date.
DateTime.TryParseExact(DateTime.Now.ToString(), DateTime.Now.GetDateTimeFormats(),
new CultureInfo("en-US"),
DateTimeStyles.None, out outDate);
or we will have to add the correct format of your date.
Or you can retrieve it directly from the wafer you use date:
string[] formats = {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt",
"MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss",
"M/d/yyyy hh:mm tt", "M/d/yyyy hh tt",
"M/d/yyyy h:mm", "M/d/yyyy h:mm",
"MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm"};
Well, DateTime.TryParseExact returns false and DateTime.MinValue (which is 01/01/0001 00:00:00) whenever it fails to parse the given string.
https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tryparseexact?view=netframework-4.8
When this method returns, contains the DateTime value equivalent to
the date and time contained in s, if the conversion succeeded, or
MinValue if the conversion failed. The conversion fails if either the
s or format parameter is null, is an empty string, or does not contain
a date and time that correspond to the pattern specified in format.
This parameter is passed uninitialized.
It seems, you want more formats to be mentioned:
// Test value
// Something like "10/08/2019 2:47:58 PM"
string value = DateTime.Now.ToString(CultureInfo.GetCultureInfo("en-US"));
...
string[] formats = {
"R", // RFC1123 e.g. 2019-10-08T14:39:47
"yyyy-M-d",
"yyyy-M-d H:m:s",
"M/d/yyyy", // en-US special: date only
"M/d/yyyy H:m:s", // en-US special: date and 24 hour time
"M/d/yyyy h:m:s tt", // en-US special: date and 12 hour time
};
DateTime outDate;
if (DateTime.TryParseExact(value,
formats,
CultureInfo.GetCultureInfo("en-US"),
DateTimeStyles.None, out outDate)) {
// Parsing succeeded
interfaceoperation.LogDate = outDate;
interfaceoperation.LogTime = outDate;
}
else
{
// Parsing failed
}

DateTime.Parse throwing string not recognized, I cannot see why

I cannot understand why this is throwing "String was not recognized as a valid DateTime"
string[] formats = {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt",
"MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss",
"M/d/yyyy hh:mm tt", "M/d/yyyy hh tt",
"M/d/yyyy h:mm", "M/d/yyyy h:mm", "MM/dd/yyyy hh:mm",
"MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm",
"MM/d/yyyy HH:mm:ss.ffffff" };
var delimit = line.Split(',');
try
{
id = delimit[0];
DateTime.TryParseExact(delimit[1].Trim(),formats,new CultureInfo("en-US"),DateTimeStyles.None, out openDate);
delimit[5] = delimit[5].Replace("\"","");
closedDate = DateTime.ParseExact(delimit[5].Trim(),formats,new CultureInfo("en-US"),DateTimeStyles.None);
DateTime.TryParseExact(delimit[5].Trim(),formats,new CultureInfo("en-US"),DateTimeStyles.None, out closedDate);
severity = delimit[7].Split('-').Last().Trim();
state = delimit[6].Trim();
}
catch(Exception e)
{
Console.WriteLine(line);
}
The entry it throws on is :
The formats array should include the 24 hour time format.
Either dd/MM/yyyy HH:mm OR MM/dd/yyyy HH:mm OR M/d/yyyy HH:mm OR d/M/yyyy H:m format can be used.
Please note that only you know whether your format is dd-MM-yyyy or MM-dd-yyyy as for both date and month places you have 12.
You can check the sample inputs to your code and depending on those you should choose the format carefully.
Hope this helps.

DateTime.ParseExact returns wrong date for the 'dd/MM/yyyy h:mm tt' format

I need to convert datetime from MM/dd/yyyy h:mm:ss tt format to the dd/MM/yyyy h:mm:ss tt
My code
var date = ((DateTime)model.WorkshopDate).ToString("dd/MM/yyyy h:mm:ss tt");
var resultDate = DateTime.ParseExact(date, "dd/MM/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
In result date = 25/12/2017 12:00:00 AM
But resultDate = 12/25/2017 12:00:00 AM .
How can i parse it right?
You are looking at the object in the debugger which is a datetime object and not a display version of your date.
Example:
var theDate = DateTime.UtcNow;
var date = theDate.ToString( "dd/MM/yyyy h:mm:ss tt" );
var resultDate = DateTime.ParseExact( date, "dd/MM/yyyy h:mm:ss tt", CultureInfo.InvariantCulture );
Console.WriteLine(date == resultDate.ToString("dd/MM/yyyy h:mm:ss tt"));//Returns true
As you see they are the same dates so just .ToString() it to whatever format you need when displaying.
You don't need to convert it to a String and back to a DateTime. The resulting DateTime object should have the same Date. You just need to convert it when you want to display it somewhere:
((DateTime)model.WorkshopDate).ToString("dd/MM/yyyy h:mm:ss tt");
here
var date = ((DateTime)model.WorkshopDate).ToString("dd/MM/yyyy h:mm:ss tt");
you are implicitly asking to display the date in "dd/MM/yyyy h:mm:ss tt"
var resultDate = DateTime.ParseExact(date, "dd/MM/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
here system default format is using to display the date.
if you want to display both as same
Console.WriteLine(date);
Console.WriteLine(resultDate.ToString("dd/MM/yyyy h:mm:ss tt"));

parse string to datetime using parse exact

I am trying to parse a string to datetime using ParseExact but I keep failing..
I tried below but received an error: String was not recognized as a valid DateTime.
string topA = "3/25/2016 12:00:00 AM";
DateTime d = new DateTime();
d = DateTime.ParseExact(topA, "dd/MM/yyyy HH:mm:ss tt", null);
Based on your string, right format should be M/dd/yyyy hh:mm:ss tt with preferable InvariantCulture.
string topA = "3/25/2016 12:00:00 AM";
DateTime d = DateTime.ParseExact(topA, "M/dd/yyyy hh:mm:ss tt",
CultureInfo.InvariantCulture);

How to convert this date time string?

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);

Categories