DateTime dt=DateTime.FromOADate(duble); //e.g 3364072679.0
My double is unfortunately larger than the allowed range for OleAut Date. What should I do then ?
If the fractional (time) part isn't important you could add the remainder to the max OLE date:
double maxOLEDate = 2958466.0
int days = duble - maxOLEDate;
DateTime dt=DateTime.FromOADate(maxOLEDate).AddDays(days);
Or just add the value to the "Min" OLE Date:
DateTime dt = DateTime.FromOADate(0).AddDays((int)duble);
Edit
I stand corrected - The DateTime structure cannot support OLE date values past 2,958,466.0 (12/31/9999). Unless H. G. Wells is your user this is not valid input.
According to the online help for ToAuthDate, the maximum OLE date is the same as DateTime.MaxValue (i.e. the end of the year 9999).
Do you really need to work with dates in the year 10,000 and beyond?
Related
I've loaded data from a delimited .doc file into an Excel workbook application using a querytable.
Subsequently I'm trying to loop through the data on the worksheet and save the data into particular variable types e.g.:
string gender = range.Rows.Cells[index + 7].FormulaLocal;
DateTime birth_date =Convert.ToDateTime(range.Rows.Cells[index + 8].FormulaLocal);
int SSN = Convert.ToInt32(range.Rows.Cells[index + 9].FormulaLocal);
I get an exception, however with the above code saying
"String was not recognized as a valid DateTime."
Now on screen in the worksheet the data in that field reads "07/09/1972"
However in the debugger I'm instead getting a value of "26489" and this seems to be the source of the exception as c# cannot convert this into a DateTime Object.
Anyone know what's going on here and how best to fix it?
When converting from Office, you must use the DateTime.FromOADate method. This is because Office uses OLE Automation date, which is a format where a floating point value is calculated, counting the days from the last of December 1899. The hours and minutes are represented as fractional days, thus adding a few decimals to the value 26489 would result in a time stamp that also represents hours, minutes and seconds.
DateTime d = DateTime.FromOADate(26489);
Console.WriteLine(d);
will output
1972-07-09 00:00:00
Excel stores dates as numbers (representing number of days and fractional days since midnight, 30 December 1899) - use FromOADate to convert to a CLR DataTime
double dbl = range.Value;
DAteTime dt = DateTime.FromOADate(dbl);
DateTime.FromOADate(Convert.ToDouble(text)).ToString("MM/dd/yyyy")
I am working on a C# project where I have a date/time in the format of 2012-11-24 15:35:18 and I need to convert this into an epoch time stamp.
Everything I've found on Google is to convert an epoch time stamp into a human readable but I need it to be done the other way round.
Thanks for any help you can provide.
I found this here:
epoch = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
Instead of DateTime.Now, you should be able to input your desired time.
You didn't say your exact use case, but the standard .NET DateTime has a Ticks attribute which is defined as:
The value of this property represents the number of 100-nanosecond
intervals that have elapsed since 12:00:00 midnight, January 1, 0001,
which represents DateTime.MinValue. It does not include the number of
ticks that are attributable to leap seconds.
This is essentially an epoch based time, if it will suit your needs. Otherwise, with this value, you should be easily able to compute a conversion to another epoch time keeping method.
You need to use TryParse:
string input = "2012-11-24 15:35:18";
DateTime dateTime;
if (DateTime.TryParse(input, out dateTime))
{
ulong epoch = (dateTime.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
}
I have a code in vb, and I'm trying convert it to c#.
_nextContactDate.ToShortDateString > New Date(1900, 1, 1)
This is _nexContractDate declaration:
Private _nextContactDate As DateTime
It's weird for me. Comapre datetime to string?
What this code is doing is extracting the date part (i.e. removing the time part) and using VB's loose nature to allow a date represented as a string to be implicitly converted back to a date for the purposes of comparison with an actual date.
The correct way to remove the time part would be to check as follows:
_nextContactDate.Date > new DateTime(1900, 1, 1)
It seems odd, as this means that the 1st Jan 1900 will fail this check, and only dates from the 2nd Jan 1900 will pass. As such, I'd be inclined to check whether this code has a logic error.
I'm not sure I understand your question entirely, but why compare a DateTime to a string anyway, why not just compare dates?
if (_nextContactDate > new DateTime(1900, 1, 1))
{
}
As noted by Greg, currently the ToShortDateString removes some parts of the date (specifically, the time units), but upon comparison with a minimum date as such, this is rather redundant - if you are concerned at such a level, then you can compare only the Date members.
no, you don't need to compare DataTime variable in string format.
you can compare like below:
DateTime myDate = new DateTime(2011,8,24);
if(myDate > DateTime.MinValue)
DoSomething();
In many databases, time is stored as the minimum date + the time value.
So assuming the minimum date is 31 Dec 1899 2359H (if I reckon right, that's the minimum for Access) then 1300H will be stored as 01 Jan 1900 1300H.
Dates are stored as 'usual'. And dates with time components have the date value with the time component attached to them.
What's this got to do with the code? The original programmer is trying to determine whether the field is holding a date or a time value. The analogy is simple. If the value is time only, then by stripping off the time component, you'll be left with 01 Jan 1900. If it contains a date component, it's going to be more than 01 Jan 1900.
I am reading Excel worksheet data using C# and Microsoft.Office.Interop. The sheet contains some date values. When I am trying to read that value it is just giving the number (probably TimeSpan). I am having problem converting this number into DateTime.
Below is the code:
TimeSpan ts = TimeSpan.Parse(((Range)ws.Cells[4, 1]).Value2.ToString());
Where ws is Excel.WorkSheet.
Can anybody explain how should I convert this number (TimeSpan) into DateTime?
Thanks for sharing your valuable time.
You could do the following
double d = double.Parse(((Range)ws.Cells[4, 1]).Value2.ToString());
DateTime conv = DateTime.FromOADate(d);
Use the following:
DateTime dt = new DateTime().Add( TimeSpan.FromMilliseconds( 1304686771794 ) )
It all depends on what the number looks like ;p That is typically the offset in some interval, into some epoch - for example seconds since 1 Jan 1970. So try, for example:
var when = new DateTime(1970,1,1).AddSeconds(number);
and then try AddMilliseconds(number), AddTicks(number) etc until the date matches.
This is just icing: Excel represents dates as OLE automation dates. These values are floating point numbers, the integer part of which is the number of days after midnight, 30 Dec 1899. Or before, if it's negative. Greco's answer gives you the best way to convert :)
How do I convert a DateTime value to a double?
If, by double you mean an OLE Automation date, then you can use DateTime.ToOADate(). From the linked MSDN topic:
An OLE Automation date is implemented as a floating-point number whose value is the number of days from midnight, 30 December 1899. For example, midnight, 31 December 1899 is represented by 1.0; 6 A.M., 1 January 1900 is represented by 2.25; midnight, 29 December 1899 is represented by -1.0; and 6 A.M., 29 December 1899 is represented by -1.25.
The base OLE Automation Date is midnight, 30 December 1899. The maximum OLE Automation Date is the same as MaxValue, the last moment of 31 December 9999.
If you're talking about some other date representation that can also be stored in a double, please specify...
DateTime.ToOADate() converts to a double OLE Automation Date in C#
Yes, OLE Automation date enable Datetime to convert to Decimal/double type. However, the outcome/value to decimal/double is not exact system Datetime.
For example,
Decimal dateIndDec = Convert.Decimal (Datetim.Today.ToOADate());
is not equal to MS SQL
Select Convert (Decimal (10, 9), GetDate())
Conclusion: OLE Automation date is not a true system datetime info... cannot use it.
I've been searched everywhere about convert Datetime value to Decimal value in C# without any luck.
You can calculate difference between your DateTime and DateTime.MinValue, and then get any Total* value.
var difference = DateTime.Now - DateTime.MinValue;
Console.WriteLine(difference.TotalMinutes);
Console.WriteLine(difference.TotalMilliseconds);