Showing posts with label DateTime. Show all posts
Showing posts with label DateTime. Show all posts

Tuesday, December 27, 2022

C# DateTime from Excel turns into float number

 In Excel, dates and times are stored as a floating point number representing the number of days since the epoch date of January 1, 1970. This means that when you read a date or time value from an Excel file into a C# DateTime object, you will need to convert the floating point value to a DateTime object.


Here is an example of how you could do this in C#:


// Assume that the Excel date value is stored in a variable called "excelDate" // Convert the Excel date value to a DateTime object DateTime dateTime = DateTime.FromOADate(excelDate); // You can also use the following syntax to achieve the same result: // DateTime dateTime = new DateTime((long)(excelDate * 86400000) + new DateTime(1970, 1, 1).Ticks);


In this example, the FromOADate method is used to convert the Excel date value to a DateTime object. This method is part of the System.DateTime struct and takes a floating point value representing the number of days since the epoch date.

Alternatively, you can use the Ticks property of the DateTime struct and some simple math to convert the Excel date value to a DateTime object. In this case, the value is first converted to a number of ticks (multiplying it by the number of ticks per day), and then the resulting ticks are



ASP.NET Core

 Certainly! Here are 10 advanced .NET Core interview questions covering various topics: 1. **ASP.NET Core Middleware Pipeline**: Explain the...