Datetime in 24 hour format - c#

my type for date in the database is datetime and format of the datetime i am getting is 2/21/2011 12:00:00 AM .how can i convert this into 24 hour format.

Rather than changing the storage mechanism of the data in table, when you displaying the data, use the format you want and show it in your User Interface. Something like this:
DateTime.Parse(dateTime).ToString("MM/dd/yyyy HH:mm")

From the comments I would suggest that you may have a mis-understanding.
After you enter datetime data into a database, it's not stored as text, it's not stored as AM/PM or 24 hour, it's its own type. Only when an application converts it for display on screen does the difference materialise. This is an artefact of the application, not the database.
Are you able to eloborate on why this is an issue to you? How is the data being used?
- Are you going to be running any code?
- Are you copying it to Excell?
- Are you just basking in it's glory?

Related

Date format problem in C#? While retrieving data from Excel

Below is the image of my code please have a look on it.
I am trying to retrieve data from excel sheet and storing it into database table through SQL bulkcopy.
Error:
The date format is 05-01-2019; it is inserted as 2019-05-01 (database) incorrectly - correct date is 2019-01-05.
When date is greater than 12 it stores in correct format.
2019-12-25 (database) correct
Excel : 25-12-2019
convert your string to a date first with
DateTime.ParseExact("25-12-1986", "dd-MM-yyyy", NULL)
then format it into your date
you can also consider TryParseExact to check for wrong format
your problem is that 05-01-2019 tends to mean 'May 1st 2019' in US style date formats, therefore you need to be very careful with formats. The policy of automatically making 25-12-1966 into 25th December (which is all it could be interpreted as) is not as helpful as it seems.

How to convert an internal numeric date to Access Date/Time format?

I get some data from a PICK/UniVerse database that includes dates in a 4 or 5 character numeric format. Here are some examples .. I grabbed the date values from the database, and compared it to the date being shown in an application:
9832 12/1/1994
10027 6/14/1995
10594 1/1/1997
Is it possible to convert these into something that can be put into Access as a Date/Time value?
As A test, I put 9832 in Excel as a General format and then change it to Short Date, it comes up as 12/1/1926. So it's off by exactly 68 years. This was true for 10027 and 10594 as well.
In C# you can use DateTime.FromOADate
DateTime dt = DateTime.FromOADate(41481);
Returns a DateTime equivalent to the specified OLE Automation Date.
That will give you:
dt = {26/07/2013 12:00:00 AM}
Later on you can insert that Date in your Access database.
Access Date/Time values are actually double precision floats. The whole number portion represents the day and the integer portion represents the time of day.
It looks like those Pick date numbers correspond directly to the date portions of Access Date/Time values. So you can use CDate to transform them.
? CDate(41481)
7/26/2013
Experiment some more to get a feel for this:
? Date()
7/26/2013
? CDbl(Date())
41481
Note, although your question is tagged with c#, you don't need that to do these conversions. You can do them with an Access query and ask the db engine to apply those functions.
Since it turned out those date numbers are consistently offset by 68 years, you can still do the conversion in an Access query.
? DateAdd("yyyy", 68, CDate(9832))
12/1/1994
? DateAdd("yyyy", 68, CDate(10027))
6/14/1995
? DateAdd("yyyy", 68, CDate(10594))
1/1/1997
Or ...
? CDate(9832 + CLng(24837))
12/1/1994
? CDate(10027 + CLng(24837))
6/14/1995
? CDate(10594 + CLng(24837))
1/1/1997
A little late to this thread but I'll post some more detail: The Pick / MultiValue DBMS stores dates as an integer with date 0 = 12/31/1967. So as I write this on Jan 16, 2014 the internal Pick date is 16818. If you use the following you'll get that magic number 24837:
DateTime.Parse("12/31/1967").Subtract( DateTime.FromOADate(0)).Days
So add that to your Pick Date to get the OADate.
If you're using any of the common MV DBMS libraries for extracting data (UniObjects, U2.NET, mv.NET ...) you shouldn't need to convert the date like this. A typical function might look like:
string date = OConv( record["PurchaseDate"], "d2/" ); // "01/16/14"
Or rather than extracting the data in the internal DBMS format, you really should be getting it in external format to start. Ask the DBMS developer who provided the data to do this for you. It's real easy on their side to return " date'd2/' " rather than just "date".
Feel free to contact me directly if you need more info in this area.
All multivalue database dates (this includes UniVerse and UniData) are based on a base date of 31st December 1967. You can resolve this to an external data in a number of ways.
The favourite - e.g. if using SQL or one of the internal database tools is to create a data dictionary entry for the field concerned with a date conversion field, For example:
'D2' for a 2-digit year
'D4' for a 4-digit year
'D4/' for a 4-digit year with slash separators
'D4/E' for a 4-digit year with slash separators and explicitly in European format (DD/MM/YYYY) as compared to US format (MM/DD/YYYY).
If no explicit formatting is given then the format will default to environmental settings. There are other formatting options as well and many can be used in combination (as with the above).
As previously advised, the alternative is to adjust the raw date with a formula. The date is in days since 31st December 1967 - The base data for all multivalue databases.

SQL Server 2012 DateTime format does not match to my application

In my SQL Server I have this date format: dd-mm-yyyy, and I have some dates matches to this format. But when I use a datagridview in my application, it parses as mm-dd-yyyy but show dd-mm-yyyy. So if I have 05.12.2012 in my database, it thinks that 05 is month so it shows as 12.05.2012.
I have tried to use
Application.CurrentCulture = new CultureInfo("tr-TR");
but it did not work.
I should change the parsing format of my application, not showing format. How can I do that?
Thank you.
You should refer to the 'SET DATEFORMAT' command to set the format of the date. However, I think you problem lies somewhere else. You should be able to access the date column as a datetime value in c#. Usually data is passed in it's native format and so you should not have problems like the one you describe.
You can also send your date in the same date format in sql from your application.
Convert your date to sql format using the culture.
This works for me as we are using different culture in one application.

Change date to Universal Time Format changes my date wrongly I can't get error

I am trying to simply change the date format from the datatable to universal time format but it formats it wrongly as if I have date for August 7 it changed it to August 8 after formatting it to universal date time. My code for formatting date is,
DateVar[runs] = DateTime.Parse(Convert.ToString(output.Tables[0].Rows[runs][0])).ToUniversalTime().ToString();
Don't get in to code its correct and its a part of loop so "run" is loop and output is data set having one table I have first data in table is "Sunday, August 07, 2011 10:52 PM" and it was converted to "8/8/2011 5:52:00 AM" after implementing universal time format.
Hopes for your suggestions
Universal time isn't a format - it's a time zone, effectively. It's not clear what you're trying to do, but converting a "local" DateTime to "universal" DateTime will usually change the time. If you don't want that to happen, don't call ToUniversalTime.
It's a pity that the .NET date/time API isn't as clear as it could be - the DateTime type itself has some horrible ambiguities about it. I'm trying to improve the situation with my Noda Time project, but you will need to understand what time zones are about etc.
Personally I would suggest not using simply DateTime.Parse or just calling ToString unless you're absolutely sure that the default format is what you want. I usually call DateTime.ParseExact and specify the expected format (and usually CultureInfo.InvariantCulture unless it's a user-entered string) - and likewise I provide a format string to the ToString call.
In your code you're simply converting a string to a string - what are you attempting to accomplish? If you're just trying to change the format (e.g. to dd/MM/yyyyTHH:mm:ss) then you don't need to call ToUniversalTime but you do need to provide the format string.
I suggest you split your code out into several statements to help you debug this (and for general code clarity):
Fetch the string from the DataTable, if you really need to (if it's already a DateTime, there's no point in converting it to a string and then back again)
Parse the string (again, assuming you need to)
Perform any conversions you need to
Format the DateTime with an explicit format string
Now if any single operation is causing a problem, you can isolate it more easily.
If I run ToUniversalTime() from Greenwich it will give same time but if i do it while I live some where else it will get an offset date time object of + or - hours depending on position.

Representing date having 32 as maximum value for Day

I am building application in asp.net using Sql server 2005. In my application I have to represent many dates & dates are of Nepali(Bikram sambhat) in which the maximum day for some month can be 32.
So what is the best option to represent the date in sql server so that 32 can be placed for day value & that can be easily compared(manipulated) in sql server as well as in asp.net?
There is a guy that has implemented some classes for converting between Nepali dates and Gregorian dates. This way you can input dates in the Nepali format but store them in a format that SQL Server understands. Look here: http://rrajbhandari.blogspot.com/2010/06/bikram-sambat-classes-and-controls.html
Remember that a date in either calendar can be converted to another calendar - they point to the same day.
I would recommend using the built in Date type. It sounds like any date in your format can be easily converted into the standard sql format.
I would just write a utility to convert any date from sql server into your display format whenever you load or write to the database.
There isn't an easy conversion from Nepali to Gregorian. Think of it in two ways:
need to show Nepali date
need to manipulate (e.g. find difference between months, days etc)
The first one is easy - store as a varchar
The 2nd one is not. If you wanted to know the difference between 2 Nepali dates, you can store the date a 2nd date as the Gregorian equivalent and use datediff/dateadd(day) between them. However, dateadd(month) will be useless for you here unless you wanted to know the difference of 2 Nepali dates in Gregorian date months - not common.
Sounds like just storing in varchar and having a library for Nepali dates, either in the front end or as a CLR, would go down better.
It may help for conversions to have a fully materialized Nepali date table with the corresponding Gregorian date, so the layout would be
NepaliYear NepaliMonth NepaliDay Gregorian
x 4 2 2014-21-23
etc
But I am not sure it would help much for (Nepali) date maths beyond conversion - and only if you need such conversion within SQL Server.
EDIT
#pst's comment
There is a reason to prefer VARCHAR to datetime. I assumed that the frequency, most of the activity is reading/writing a Nepali date - which is stored and read as a VARCHAR - no conversion. If and only when date maths is required does it involve the library - in which case you invoke conversion, and only then if you need at any point in time the Gregorian calendar equivalent. If all you wanted is maths between NepaliDate/scalar or NepaliDate/NepaliDate - again, datetime offers no benefit whatsoever - it cannot handle day #32 in a month.
Check out Nepali calendar, it may help.

Categories

Resources