This question already has answers here:
Convert string to DateTime in c#
(3 answers)
Closed 6 years ago.
I have a string that is a date in the format of "MMddyyyy". I need to convert it to a DateTime with formatting "dd/MM/yyyy". I created the following code, but I keep getting a Format Exception. Parse, ParseExact, TryParseExact all give a Format Exception also.
string dater = "10312016";
DateTime condate = Convert.ToDateTime(dater);
I have tried the following:
string dater = "10312016";
DateTime condate = DateTime.ParseExact(dater, "MMddyyyy", CultureInfo.InvariantCulture);
DateTime condate2 = DateTime.ParseExact(dater, "yyyyMMdd", CultureInfo.InvariantCulture);
condate works, but it is in the wrong format. It needs to be "dd/MM/yyyy"
string dater = "10312016";
DateTime condate = Convert.ToDateTime(dater).tostring("dd/MM/yyyy");
Just try this. I am posting answer from my mobile. So tostring syntax may not right. When u type tostring in IDE then it will be alright.
Edit
sorry for later answer. see this code and it is working.
string dater = "10312016";
DateTime condate = DateTime.ParseExact(dater, "MMddyyyy", System.Globalization.CultureInfo.InvariantCulture);
string strDate = condate.ToString("dd/MM/yyyy");
Th
anks
Related
This question already has answers here:
How to convert a date string "dd/MM/yyyy" format into "MM/dd/yyyy" in asp.net c#?
(4 answers)
Closed 6 years ago.
I have a string containing information 08/23/2016~08:00 - 12:00~D . I want to first convert it into date i.e 08/23/2016 and then convert this into Tuesday,August 23,2016 . Is it possible to convert it in this format?
A combination of string- and DateTime-methods
string input = "08/23/2016~08:00 - 12:00~D";
string datePart = input.Split('~')[0].Trim();
DateTime dt;
if (DateTime.TryParse(datePart, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out dt))
{
string output = dt.ToString("dddd,MMMM dd,yyyy", DateTimeFormatInfo.InvariantInfo);
}
Custom Date and Time Format Strings
Try this way
string inp = "08/23/2016~08:00 - 12:00~D";
string str = inp.Split('-')[0].Trim();
DateTime date = DateTime.ParseExact(str, "MM/dd/yyyy~hh:mm", CultureInfo.InvariantCulture);
string s1 = date.ToString("MM/dd/yyyy");
string s2 = date.ToString("dddd,MMMM dd,yyyy");
This question already has answers here:
Converting a String to DateTime
(17 answers)
Closed 6 years ago.
i am trying to convert this string i get from the customer to a datetime, to store it in my db.
i tried several approches but nothing seems to work.
i need the offset in UTC time so i guess i need to do something with zz.
This is working:
string test = "2008-06-11T16:11:20.0904778Z";
DateTime publishingTimeStamp = DateTime.ParseExact(test, "o", CultureInfo.InvariantCulture,
DateTimeStyles.None);
But the string by the customer is slightly different, so i tried this one:
string test = "2017-01-01T12:00:33:123456Z+02";
DateTime publishingTimeStamp = DateTime.ParseExact(test, "yyyy-MM-dd'T'HH:mm:ss:ffffffzz", System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None);
Unfortunaly it is not working. How can i convert the exact string "2016-01-01T10:00:55:123456Z+02"? Is it even possible because it uses ":" in front of ffffff
Thank you
Looks like you forget to escape Z as a string literal delimiter in your format as 'Z'.
string test = "2017-01-01T12:00:33:123456Z+02";
DateTime publishingTimeStamp = DateTime.ParseExact(test,
"yyyy-MM-dd'T'HH:mm:ss:ffffff'Z'zz",
CultureInfo.InvariantCulture, DateTimeStyles.None);
Console.WriteLine(publishingTimeStamp); // 01.01.2017 12:00:33
But since your string has UTC Offset value, I would parse it to DateTimeOffset instead.
string test = "2017-01-01T12:00:33:123456Z+02";
DateTimeOffset publishingTimeStamp = DateTimeOffset.ParseExact(test,
"yyyy-MM-dd'T'HH:mm:ss:ffffff'Z'zz",
CultureInfo.InvariantCulture,
DateTimeStyles.None);
Now you have a DateTimeOffset as {01.01.2017 12:00:33 +02:00}
This question already has answers here:
Converting a string to DateTime object
(5 answers)
Closed 7 years ago.
I have a string which value is "2016-01-07 20:43:01,803".
I'd like to convert it use DateTime.Parse method. it is failed.
How to convert to datetime with this type of string?
You could use the ParseExact method.
var input = "2016-01-07 20:43:01,803";
DateTime dt = DateTime.ParseExact(input, "yyyy-MM-dd HH:mm:ss,fff", CultureInfo.InvariantCulture);
Try to use DateTime.ParseExact with the right format. (the last phrase is very very important: right format)
DateTime dt = DateTime.ParseExact("2016-01-07 20:43:01,803", "yyyy-MM-dd HH:mm:ss,fff", null);
This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 7 years ago.
I need to convert a string with date to valid c# date time object.
input string example = "2015-03-24T12:31:33.8700000"
output c# datetime
I tried doing this
DateTime.ParseExact(x.claimDetails.clmDOA, "yyyy-MM-dd HHmmss", CultureInfo.InvariantCulture)
Buit it gave exception
String was not recognized as a valid DateTime
Please Note: I googled thoroghly . Somehow i couldnt find any string
with "T" included as in datetime string.
Prior to downvoting if one can suggest me exactly where a question is answered with datetime string containg a T in it or of this format.yyyy-MM-dd'T'hh:mm:ss.fffffff
DateTime dt = DateTime.Parse(example);
DateTime dt = DateTime.ParseExact(example, "yyyy-MM-dd'T'hh:mm:ss.fffffff", CultureInfo.InvariantCulture);
Should work for you.
From DateTime.ParseExact 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 or an exception is thrown.
In your case, they are not.
You need to use T as a string literal delimiter, specify : as a TimeSeparator and seconds fraction (with fffffff specifier) as well.
var dt = DateTime.ParseExact("2015-03-24T12:31:33.8700000",
"yyyy-MM-dd'T'HH:mm:ss.fffffff",
CultureInfo.InvariantCulture);
DateTime.ParseExact(example , "yyyy MM dd HH:mm:ss", CultureInfo.InvariantCulture);
This question already has answers here:
Display only date and no time
(11 answers)
Closed 8 years ago.
i m getting date from sql server database and it is displaying datetime as 1/15/2015 12:00:00 AM.
code in c# i used is:
dob_lbl.Text = reader[6].ToString();
//this is extracted using Sqlconnection so it's in array format.
i need only date to display.pls help.
try this:
string strDate = reader[6].ToString();
dob_lbl.Text = DateTime.ParseExact(strDate, "M/dd/yyyy hh:mm:ss tt",
CultureInfo.InvariantCulture).ToString("yyyy/MM/dd");
The ToShortDateString method of DateTime should help with this.
Use it like:
string strDate = reader[6].ToString();
DateTime dateTime = DateTime.Parse(strDate);
string justDateStr = dateTime.ToShortDateString();
You could simply split the string and use the first part of the resulting array.
Something like:
dob_lbl.Text = reader[6].ToString().Split()[0];