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
Related
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:
How do I convert an Excel serial date number to a .NET DateTime?
(4 answers)
Closed 4 years ago.
I am trying to parse a integer(like 43392) to a date time.
The code looks like this:
DateTime d = DateTime.ParseExact(item[11], "dd/MM/yyyy", CultureInfo.InvariantCulture);
wher item[11] is "43392".
This throws an System.FormatException error, string was not recognized as a valid Datetime.
Need some guidance on this.
This should work
DateTime dateTime = DateTime.FromOADate(43392);
Output
19/10/2018 12:00:00 AM
DateTime.FromOADate Method (Double)
Returns a DateTime equivalent to the specified OLE Automation Date.
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:
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)
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.