use TryParseExact to format an integer value as a time - c#

I have an integer that will have the value of a year month and day. For example 20110504.
I am using TryPareseExact to format it into yyyy-MM-dd but it is not working.
Here is my function
public DateTime DateDisplay(int date)
{
DateTime dateValue;
if (DateTime.TryParseExact(date.ToString(), "yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out dateValue))
return dateValue;
else
return DateTime.MinValue;
}
It always go to the else and returns DateTime.MinValue. I want the date to be look like 2011/05/04. Would you be able to help me to identify where is my mistake?

I believe the format string should be "yyyyMMdd" if your input string has no hyphens.

Related

convert date string coming in two different formats YYYYMMDD and DDMMYYYY to date in c#

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

Converting string dd/mm to datetime

Converting a String to DateTime
As the above link says I can do conversion if I'm having a the complete dd/mm/yyyy,But I'm having only dd/mm not the year field.
I have achieve it by changing the date to mm/dd format and using Convert.ToDateTime(date).So any help please.
You can parse that string. Just remember that the Month part is MM not mm (minutes)
string data = "01/01";
DateTime dt;
DateTime.TryParseExact(data, "dd/MM", CultureInfo.CurrentCulture, DateTimeStyles.None, out dt);
Console.WriteLine(dt.ToLongDateString());
Of course the missing year is assumed to be the current year
You can use this source to learn more about specifiers for parsing custom date's.
Put your string variable instead of CustomDate field.
DateTime d = DateTime.ParseExact(CustomDate, "dd/MM",System.Globalization.CultureInfo.CurrentCulture);
I would use the function DateTime.TryParseExact since you can use it within an If - else structure very easily
private DateTime date;
private myString = "23/04";
if (DateTime.TryParseExact(myString, "dd/MM", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
{
myDate = date;
}
else
{
//do nothing
}
With this you can catch errors when parsing the string.

C# (Windows Phone Silverlight) String to DateTime

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

Convert custom date to mysql datetime

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.

How convert a string into Datetime in c#

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

Categories