This question already has answers here:
Converting a String to DateTime
(17 answers)
Closed 7 years ago.
I receive DateTime in this format => yyyyMMddHHmmss e.g. 20160214204032
Due to its somewhat unique format, I can't just use Convert.ToDateTime -- I tried, it didn't work.
It's easy enough to create a helper method that would parse the components of this date e.g
var year = myString.Substring(0,4);
but I'm concerned that this may have poor performance.
Can anyone think of a better way to convert a string in this format to DateTime?
You cannot set format in Convert.ToDateTime. So, use ParseExact instead.
DateTime.ParseExact("20160214204032", "yyyyMMddHHmmss",
System.Globalization.CultureInfo.InvariantCulture)
Due to its somewhat unique format, I can't just use Convert.ToDateTime -- I tried, it didn't work.
It fails because Convert.ToDateTime tries to convert from your system Date Time Format and throws exception if it can't.
using String Functions is also bad to convert to DateTime so you can do
DateTime dt = DateTime.ParseExact("20160214204032",
"yyyyMMddHHmmss",
System.Globalization.CultureInfo.InvariantCulture)
Related
This question already has answers here:
Parse string to DateTime in C#
(9 answers)
Closed 3 years ago.
In the above code, I am getting the date data from SpreadsheetDocument. The exampled date cannot be converted.
But other dates can be converted.
Why cant i convert this date and how can I convert it in different way?
DateTime.FromOADate(double.Parse("05.09.1977"));
I want to convert this string to DateTime with this.
"05.09.1977 is not a valid double. It looks like it's an actual date (either MONTH.DAY.YEAR or DAY.MONTH.YEAR).
To parse it to a DateTime use either:
DateTime.ParseExact("05.09.1977", "dd.MM.yyyy", CultureInfo.InvariantCulture)
or
DateTime.ParseExact("05.09.1977", "MM.dd.yyyy", CultureInfo.InvariantCulture)
This way:
Console.WriteLine(DateTime.ParseExact("05.09.1977", "dd.MM.yyyy", CultureInfo.InvariantCulture));
Output:
05/09/1977 00:00:00
according to the
DateTime.FromOADate(Double) method doc
Returns a DateTime equivalent to the specified OLE Automation Date.
public static DateTime FromOADate (double d);
Parameters
d
Double
An OLE Automation Date value.
so, you have to pass a double as argument
but double.Parse("05.09.1977")???
how can that be a double??
that is the reason
This question already has answers here:
Converting a String to DateTime
(17 answers)
Parse a string containing date and time in a custom format
(3 answers)
Closed 4 years ago.
I have a string: "20180830" which represents 30 august 2018
I want to go to string: "30/08/2018"
So that I can do: DateTime parsedDate = DateTime.Parse("30/08/2018"); and have a DateTime instead of a string,
Tried everything but didn't succeed.
Needs some help.
You can use the DateTime.ParseExact-Method to solve your problem. Therefore you need to specify the exact format which would be yyyyMMdd in your case. Also the documentation suggests to use CultureInfo.InvariantCulture.
The following code...
DateTime datetime = DateTime.ParseExact("20180830", "yyyyMMdd", CultureInfo.InvariantCulture);
...should do the trick ;-)
This question already has answers here:
Convert YYYYMMDD string to MM/DD/YYYY string
(4 answers)
Closed 5 years ago.
I am using a stored procedure which has DateTime in yyyy/MM/dd format, but I want to convert that in my code behind into dd/MM/yyyy format. Can anyone help me with this?
txtstart.Text = "01" + "/01/" + DateTime.Now.Year.ToString() ;
txtend.Text = System.DateTime.Now.ToString("dd/MM/yyyy");
try this.
txtend.Text = DateTime.ParseExact("your date", "dd/MM/yyyy",
CultureInfo.InvariantCulture);
I am assuming that you are getting DateTime in yyyy/MM/dd format from a database and wanted to convert it to dd/MM/yyyy. If so, see if the below one helps you,
string dateTimeFromDB = System.DateTime.Now.ToString("yyyy/MM/dd");
string dateTimeInCorrectFormat =
Convert.ToDateTime(dateTimeFromDB).ToString("dd/MM/yyyy");
However, check for FormatException and other needed defensive checks before and while formatting.
Use DateTimes ParseExact Method to convert your original string to a DateTime value and then use ToString with a pattern to convert that DateTime object to string in your needed format:
DateTime.ParseExact(yourDateString, "dd/MM/yyyy", null).ToString("MM/dd/yyyy")
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
In C#, given a DateTime object, how do I get a ISO 8601 date in string format?
I've a variable with type DateTime in my C# code.
How do I convert this to "Z" format?
Thanks.
Do you mean converting to a string time/date like 2009-06-15 20:45:30Z? If so then try:
mydate.ToUniversalTime().ToString("u");
See http://msdn.microsoft.com/en-us/library/az4se3k1.aspx for info on Standard Date and Time format strings. In particular read the entry on using the "Universal Sortable ("u") Format Specifier" whcih explains why I have the ToUniversalTime call in there.
If you mean the universal sortable date/time pattern:
string formatted = theDate.ToString("u");
Example result:
2009-06-15 20:45:30Z
Based on a quick search it seems to me that you simply mean UTC time, in which case you can use the ToUniversalTime method of DateTime, and when displaying as a string append "Z" to the end, or use the Universal Format Specifier when calling ToString, or you could construct it using a number of format specifiers in the correct order.
I guess your format is ISO 8601: DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ")
Maybe a duplicate of this topic: Given a DateTime object, how do I get an ISO 8601 date in string format?
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Converting .NET DateTime to JSON
How can I convert a date value in "mm/dd/yyyy hh:mm:ss" format to "/Date(1324414956395)/" format (Json Date).
I am passing the date format "mm/dd/yyyy hh:mm:ss" into a MVC controller action method and I need to compare that to another date in JsonDate format in the code.
Thanks for help.
dt.ToUniversalTime() won't be recognised when using DateTime?. DateTime? is essentially Nullable<DateTime>. What you need to do is use the Value property to retrieve the DateTime object
dt.Value.ToUniversalTime();
You can then use the code from this post (Jeff Meatball Yang's answer, not the accepted answer) with your nullable DateTime.
(DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
Or
This superior solution from this link: Converting .NET DateTime to JSON
DateTime d1 = new DateTime(1970, 1, 1);
DateTime d2 = dt.ToUniversalTime();
TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
return ts.TotalMilliseconds;
It seems that this is a bad way to compare dates, however. This is basically counting the ticks since 1970 (or something) and is going to be a pretty long and accurate number. Even if you needed to make certain that the dates matched down to a second it seems like it would be easier to convert all the universal time to mm/dd/yyyy format and then compare at that point.