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);
Related
I want to have date in specific forma dd-mm-yyyy 15-05-2018 and my input date format on textbox is also dd-mm-yyyy in backend code c# i am converting this date into DateTime for input validation purpose.
DateTime dDateOfBirth;
if (!string.IsNullOrEmpty(txtDOB.Text))
{
dDateOfBirth= DateTime.ParseExact(txtDOB.Text, "dd-mm-yyyy",null);
}
When i check dDateOfBirth it stores shows me dates as 05/21/2018 while i want to pass it as 21-05-2018
Not sure what i am doing wrong
Using
DateTime.ParseExact(txtDOB.Text, "dd-MM-yyyy",null); also gives me same result MM/dd/yyyy
When you check the parameter dateOfBirth it will display in whatever is set as your machines culture. The date can be then formatted as a string, that you can customise.
Internally dates are handled as standardised objects, you can't change that. Effectively you are doing nothing wrong. It the same reason that if you were to inspect double dbl = 0.00 you would see dbl is "0" not "0.00.
If you wanted to set the text in the text box you would use:
txtDOB.Text = dDateOfBirth.ToString("dd-MM-yyyy").
For more info on formatting date time to strings see: https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings and https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings
Also see: https://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx
Especially
The DateTime value type represents dates and times with values ranging
from 00:00:00 (midnight), January 1, 0001 Anno Domini (Common Era)
through 11:59:59 P.M., December 31, 9999 A.D. (C.E.) in the Gregorian
calendar.
Time values are measured in 100-nanosecond units called ticks, and a
particular date is the number of ticks since 12:00 midnight, January
1, 0001 A.D. (C.E.) in the GregorianCalendar calendar (excluding ticks
that would be added by leap seconds). For example, a ticks value of
31241376000000000L represents the date, Friday, January 01, 0100
12:00:00 midnight. A DateTime value is always expressed in the context
of an explicit or default calendar.
So from this, you can take away that a date is just a really big number. When inspecting a date using breakpoints / watches etc, VisualStudio displays that as something more friendly for us mere humans.
In the dd-mm-yyyy format, mm should be MM. So try with MM.
Try This :
DateTime dateTime = new DateTime();
dateTime = Convert.ToDateTime(DateTime.ParseExact("YouDateString", "dd-MM-yyyy", CultureInfo.InvariantCulture));
I use following code to make it work in dd-MM-yyyy format, There may be other ways to do it i just wanted to it simple to one line of code.
if (!string.IsNullOrEmpty(txtDOB.Text))
{
dDateOfBirth= DateTime.ParseExact(txtDOB.Text, "dd-MM-yyyy",null);
//dDateOfBirth is in MM/dd/yyyy format
//so i am using next statement to convert to dd-MM-yyyy format
string sdDateOfBirth = dFromDate.ToString("dd-MM-yyyy");
}
I am trying to format some precise dates, converting them from a Unix timestamp to a DateTime object. I noticed that the AddSeconds method has an overload that accepts a floating point number.
My expectation is that I may pass in a number such as 1413459415.93417 and it will give me a DateTime object with tick-level precision. Is this a decent assumption, or does the AddSeconds method still provide no better than millisecond precision? In the conversion, do I have to add the ticks myself?
My conversion code is below:
public static DateTime CalendarDateFromUnix(double unixTime)
{
DateTime calendarTime = UnixEpoch.AddSeconds(unixTime);
return calendarTime;
}
I expect to format the ToString of this date like 16 Oct 2014 11:36:55.93417 using the format string below:
dd MMM yyyy HH:mm:ss.fffff
Instead of giving me 16 Oct 2014 11:36:55.93417, it is giving me 16 Oct 2014 11:36:55.93400
Am I doing something wrong or is .NET truncating my floating-point seconds representation? I am new to .NET, so the former is quite possible.
Thanks
From the documentation of DateTime.AddSeconds:
The value parameter is rounded to the nearest millisecond.
An alternative would be to multiply by TimeSpan.TicksPerSecond and then add that to the ticks of UnixEpoch:
return new DateTime(
UnixEpoch.Ticks + (long) (unixTime * Timespan.TicksPerSecond),
DateTimeKind.Utc);
I assumed that DateTime.FromOADate in .NET and casting to a DateTime in MS SQL worked the same way.
However, given the value: 41640
DateTime.FromOADate(value) returns: 2014-01-01
CAST(value AS DATETIME) returns: 2014-01-03
Is this expected behaviour because of different starting days, or is something not right?
This is the third day of January in 2014 in T-SQL:
SELECT CAST(41640 AS DATETIME)
and this is the first day of January in 2014 in .NET:
DateTime dt = DateTime.FromOADate(41640)
The reason is documented in MSDN:
CAST
The "zero"-date is 1900-01-01
DateTime.FromOADate
base date, midnight, 30 December 1899
So there is a two days difference between 01/01/1900 and 12/30/1899.
To investigate this you have to look into the base date first,
In MSSQL print CAST(0 AS DATETIME) will output:
Jan 1 1900 12:00AM
In C# .Net Console.WriteLine(DateTime.FromOADate(0)); will output:
12/30/1899 12:00:00 AM
So you can see there are 2 days of difference between 2 base date. That's why you are facing such problem.
OLE Automation Dates (aka "OADates") are for compatibility with COM interfaces, and used in communicating to things like Microsoft Excel through VBA. You shouldn't use them in communicating with SQL Server. Just return the native SQL date, datetime, or datetime2 type in your query and cast it to a DateTime in your .NET code.
DateTime dt = (DateTime) myDataReader["FooDateTime"];
As others have mentioned, the SQL Server epoch is not the same as the OLE Automation epoch. OLE Automation dates also have some quirky behaviors with negative values, and also that dates before March 1st 1900 might use an epoch of 12/30/1899 or 12/31/1899 depending on which program is using it. SQL Server uses a fixed epoch of 1/1/1900.
And like many of Windows and .NET types, the epoch isn't fixed to UTC, so you have to know what contextual time zone information is in play also. (Though this also occurs with DateTime unless you pay attention to the .Kind property.)
SQL Server's base date is '19000101'; try CASTing 0.
According to this: http://msdn.microsoft.com/en-us/library/system.datetime.fromoadate.aspx
FromOADate starts at 1899-12-30
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?
Is there any difference between DateTime in c# and DateTime in SQL server?
Precision and range (so, everything important ;-p)
From MSDN:
.NET System.DateTime
The DateTime value type represents dates and times with values ranging from 12:00:00 midnight, January 1, 0001 Anno Domini (Common Era) through 11:59:59 P.M., December 31, 9999 A.D. (C.E.)
Time values are measured in 100-nanosecond units called ticks, and a particular date is the number of ticks since 12:00 midnight, January 1, 0001 A.D. (C.E.) in the GregorianCalendar calenda
Transact SQL datetime
Date Range: January 1, 1753, through December 31, 9999
Accuracy: Rounded to increments of .000, .003, or .007 seconds
You can also use datetime2 of SQL Server 2008. The precision there is 100ns as well. In fact, it was introduced to match the .NET DateTime precision.
datetime2 (Transact-SQL)
Yes.
The C# equivalent of the SQL datetime type is SqlDateTime
So, define the SQL call (stored procs with parameter collection of course) to use SQLDateTime. On advantage is you can trap any overflow or out of range error building the command rather than at execution time from the database engine.