Excel AddIn, C#, UDF,
MyUDF calls a web service to retrieve certain date. Sometimes the date returned is not in range of Jan-1-1900 to Dec-31-9999 (Excel Date Range). E.g. in one case returned date is Jan-2-0002 (valid in C# but not valid in Excel), then Excel crashes. I do not hard code "Jan-1-1900", so wonder if there is a way that I can get it programmatically. thanks
Inside Excel, convert the date corresponding to 1 into a string that you can parse:
=TEXT(1,"dd-mm-yyyy")
which should give you 01-01-1900 or 02-01-1904 depending on the date system chosen (hat tip to barrowc!). Note how the date systems don't only differ by four years but also by the fact that in one case the value 1 corresponds to January 1st (in 1900) and in the other it is 0 which corresponds to January 1st (in 1904). For a given Workbook, the Workbook.Date1904 Property can tell you which date system is being used.
Related
Basically I have been tasked with editing the company database for dates field known as 'CreateDt'.
However the dates with in this field are stored in numerous amounts of formats which need to be converted into one, some examples are shown below;
29/01/2001 08:51:56
29/01/2001 12:09:07
05/06/2002
30/01/2001 11:48:53
31/01/2001 09:30:12
11/12/2014
12 December 2014
15/12/2014
15/12/2014
15 December 2014
15/12/2014
I have previously tried SQL server to convert however I have encountered problems when day and month values are less than 13 and dates are changing from
for example
11/12/2014
to
21/11/2014
Would anyone have C# code which would enable me to format all dates accordingly
There is no such code because simply put, a computer cannot do, what a human cannot define.
11/12/2014
Can you tell me whether that is "November 12th" or "11th of December"? Probably not and that's not surprising. But neither can a computer.
Your best bet is to gather all formats that you think are applicable. For example for the value above, you need to decide which format is correct and which value you want to get. Then convert all your data with those few formats.
I need to convert a C# datetime object into the dreaded Excel date format:
https://datapub.cdlib.org/2014/04/10/abandon-all-hope-ye-who-enter-dates-in-excel/
i.e. number of days since 1 Jan 1900 expressed as a floating point number.
Is there any way to do it without resorting to DIY code?
I need it in order to create Excel-friendly CSV exports
Googling around I didn't find anything useful except that good blog post
Excel dates use the OLE Automation date format. You can retrieve it with DateTime.ToOADate
OA Dates are a double whose integer part is the date offset from 30 December 1899 (ie earlier dates are negative) and fractional part is the time divided by 24.
This type was used a lot in the COM/VB6 days. Nowadays it's needed for Excel and when you need to call COM APIs that expect dates or variants with a date content.
You can use following method to convert from Excel Date back to C# DateTime
return DateTime.FromOADate(SerialDate);
In an Excel cell, I put 12, if I format it as Date, then it is 1/12/1900.
In C#, I use DateTime.FromOADate(12), it returns 1/11/1900.
but if I put 411 in Excel and format it as date, it will be 2/14/1901.
In C#, DateTime.FromOADate(411) returns 2/14/1901, too.
I am confused about the discrepancy. How can I get the right Date in C# then?
This is an Excel quirk where it emulates a Lotus 1-2-3 bug for compatibility.
The year 1900 was not a leap year, but Excel treats it as a leap year to be compatible with the Lotus 1-2-3 bug. The OLE date/time processing correctly does not.
There is this amusing anecdote about the issue: http://www.joelonsoftware.com/items/2006/06/16.html
If you need to work around this and emulate the Excel behaviour before 1 March 1900, you can set a double instead of a date, and do a DateTime to double conversion that respects the Excel bug too. Internally Excel always represents the dates as doubles anyway.
I'm making a library that uses OpenXML in C# to read excel files. I can read a cell text and numbers just fine, but when it comes to dates there's a problem.
There's the type "date" for the cells, but apparently Excel 2007 doesn't save the dates in that type, so I can't tell if the value I'm reading is a date or not, instead it appears to use styles.
How could I detect if it is a date and return the string representation of it (ex: 29-12-2010)?
Excel stores dates as a float value... the integer part being the number of days since 1/1/1900 (or 1/1/1904 depending on which calendar is being used), the fractional part being the proportion of a day (ie the time part)... made slightly more awkward by the fact that 1900 is considered a leap year.
The only thing that differentiates a data from a number is the number format mask. If you can read the format mask, you can use that to identify the value as a date rather than a number... then calculate the date value/formatting from the base date.
I am using SQL Server 2008 and I have a database file (mydata.mdf) in which I have a column in one of the table and that column has the datatype (Time) .
I added this database file to my WPF-C# project (using VS2010) , but I had a problem and its as follows :
This (Time) column treats time in 24-Hours system , but I want to use (12-Hour) system in my application , so is there a way to define a 12-Hour system time in SQL server2008 .
and if there isn't , what do you think is the best way to handle that ???
Pleeeeeeeeeeease help me ASAP because I'm in a hurry and I can't figure it out ...
The time in the database is not "formatted". It is represented in some internal format (which you can Google for but shouldn't care about) that allows it to represent each moment in the day, to the supported level of precision.
The values are only formatted when your application converts them to strings for the purpose of displaying them to the user, and you have full control over this.
So if you have read a time into an instance of the CLR DateTime class, you can display as a 12-hour time (omitting the date) with value.ToString("h:mm:ss tt"). Custom formatting options are listed here.
The answer is you can't really, but don't worry, it's not a problem. Format the date in your C# code.
The point is that a date and time is an absolute value, which is what you want SQL to store, then 12 hour vs 24 hour clock is merely a display detail, eg, 13:00 and 1:00pm are equivalent, don't worry about how SQL stores it, then in C# use the following to display it:
DateTime myDateTime = GetTheTimeFromSomeMethod();
myDateTime.ToString("h:mm:ss tt");
There are lots of guides, This is a good one, but there are plenty of others eg this one
Does it have to be formatted from the database? C# and WPF both provide many built-in date format options. For example, check out the ContentStringFormat property on a Label.
If you must do it in the database, here is a messy workaround which will work
It formats the date as a string using a 12h clock, then removes the date part of it
select right(convert(varchar, cast('1/1/2010 23:59:59' as datetime), 100),
charindex(' ', reverse(convert(varchar, cast('1/1/2010 23:59:59' as datetime), 100)))-1)