1

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.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • 3
    "05.09.1977" is not something that can be converted to a `double`, try to parse it directly to a `DateTime`: https://learn.microsoft.com/en-us/dotnet/api/system.datetime.parseexact?view=netframework-4.7.2 – Caramiriel Apr 12 '19 at 10:57
  • Just use DateTime.Parse("05.09.1977") which defaults to a DateTime with day one 1/1/1. If you want a OLE date where day one is 1/1/1900 then take the DateTime object and use ToOADate. – jdweng Apr 12 '19 at 11:03
  • 1
    Why it has +2 ? he is asking why something which is not double can't be parsed to double ... – Selvin Apr 12 '19 at 11:03
  • Instead trying `string_with_date -> double -> date` you should `string_with_date -> date` – Cleptus Apr 12 '19 at 11:15

3 Answers3

4

"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)
Sean
  • 60,939
  • 11
  • 97
  • 136
3

This way:

Console.WriteLine(DateTime.ParseExact("05.09.1977", "dd.MM.yyyy", CultureInfo.InvariantCulture));

Output:

05/09/1977 00:00:00

Marco Salerno
  • 5,131
  • 2
  • 12
  • 32
  • 1
    You are assuming 05 is the day and 09 is the month, it could be just the other way. I'm voting in favor of Sean's answer – Cleptus Apr 12 '19 at 11:48
1

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

Cleptus
  • 3,446
  • 4
  • 28
  • 34
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97