473,698 Members | 2,571 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to convert a date represented as a long in database

Hi all,

Hope everybody is having an OK monday?!!?

Quick question, I have a database in which the dates are stored as
longs. I think the original program was VB and the dates were just
cast to long before being saved to the database.

I now need to convert these long values back into datetime values.

I could do this in SQL by doing:
PRINT Convert(varchar , Convert(datetim e, Convert(int, '38875')-2))

but was wondering if there was a C# way?

Cheers,

OO

Nov 17 '05 #1
6 6733
Provided VB and .NET use the same starting date, which is likely, there is an
overload on the DateTime constructor that accepts a long. If not, set up a
DateTime with the starting date for VB and add the long in a timespan object.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************** ************
Think Outside the Box!
*************** ************
"Richard" wrote:
Hi all,

Hope everybody is having an OK monday?!!?

Quick question, I have a database in which the dates are stored as
longs. I think the original program was VB and the dates were just
cast to long before being saved to the database.

I now need to convert these long values back into datetime values.

I could do this in SQL by doing:
PRINT Convert(varchar , Convert(datetim e, Convert(int, '38875')-2))

but was wondering if there was a C# way?

Cheers,

OO

Nov 17 '05 #2
You have to be careful here, because you have to make sure that SQL
Server and .NET use the same starting date (if the OP is pulling the values
form the database first before converting).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Cowboy (Gregory A. Beamer) - MVP" <No************ @comcast.netNoS pamM> wrote
in message news:E4******** *************** ***********@mic rosoft.com...
Provided VB and .NET use the same starting date, which is likely, there is
an
overload on the DateTime constructor that accepts a long. If not, set up a
DateTime with the starting date for VB and add the long in a timespan
object.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************** ************
Think Outside the Box!
*************** ************
"Richard" wrote:
Hi all,

Hope everybody is having an OK monday?!!?

Quick question, I have a database in which the dates are stored as
longs. I think the original program was VB and the dates were just
cast to long before being saved to the database.

I now need to convert these long values back into datetime values.

I could do this in SQL by doing:
PRINT Convert(varchar , Convert(datetim e, Convert(int, '38875')-2))

but was wondering if there was a C# way?

Cheers,

OO

Nov 17 '05 #3
"Richard" <wh****@hotmail .co.uk> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com...
Quick question, I have a database in which the dates are stored as
longs. I think the original program was VB and the dates were just
cast to long before being saved to the database.

I now need to convert these long values back into datetime values.


I believe that the VB date is the number of days since 30th December 1899,
so the following ought to work (but I haven't tried it).

DateTime startDate = new DateTime(1899, 12, 30);
DateTime dateWanted = startdate.AddDa ys(valueFromDat abase);

Chris Jobson
Nov 17 '05 #4
The System.DateTime has a constructor which takes a long value, is this
what you looking for?

Richard wrote:
Hi all,

Hope everybody is having an OK monday?!!?

Quick question, I have a database in which the dates are stored as
longs. I think the original program was VB and the dates were just
cast to long before being saved to the database.

I now need to convert these long values back into datetime values.

I could do this in SQL by doing:
PRINT Convert(varchar , Convert(datetim e, Convert(int, '38875')-2))

but was wondering if there was a C# way?

Cheers,

OO

Nov 17 '05 #5
Hi,

I don't think so, DateTime start at midnight of year 1 , I bet VB does not,
the same thing with SQL, IIRC it's around 1899.
cheers,

"Cowboy (Gregory A. Beamer) - MVP" <No************ @comcast.netNoS pamM> wrote
in message news:E4******** *************** ***********@mic rosoft.com...
Provided VB and .NET use the same starting date, which is likely, there is
an
overload on the DateTime constructor that accepts a long. If not, set up a
DateTime with the starting date for VB and add the long in a timespan
object.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************** ************
Think Outside the Box!
*************** ************
"Richard" wrote:
Hi all,

Hope everybody is having an OK monday?!!?

Quick question, I have a database in which the dates are stored as
longs. I think the original program was VB and the dates were just
cast to long before being saved to the database.

I now need to convert these long values back into datetime values.

I could do this in SQL by doing:
PRINT Convert(varchar , Convert(datetim e, Convert(int, '38875')-2))

but was wondering if there was a C# way?

Cheers,

OO

Nov 17 '05 #6
Hi,

Thanks for your messages!

Although there is an overload for DateTime that accepts a long, this
will always throw an InvalidCastExce ption according to MSDN.
I know VB went from 1899 Dec 30th whereas SQL starts at 1900 Jan 1 so I
would have to calculate for that. I think the timespan as suggested
above is the way to go.

Cheers all,

Richard

Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
9292
by: perplexed | last post by:
How do you convert a user inputted date to a unix timestamp before insterting it into your database? I have a form, with a textfield for a date that the user inputs in the format mm-dd-yyyy and three dropdow boxes for hours, minutes, and AM/PM. All of these need to be considered together and converted to one Unix Timestamp and then inserted to the MYSQL date field. The type of field is INT (11) so that I can instead of the standard...
19
7284
by: Lauren Quantrell | last post by:
I have a stored procedure using Convert where the exact same Convert string works in the SELECT portion of the procedure but fails in the WHERE portion. The entire SP is listed below. Specifically, I have a problem with this portion in the WHERE clause: DATEADD(Day,tblMyEventTableName.ReminderDays, @DateNow) Between CONVERT(smalldatetime,str(DATEPART(Month, @DateNow)+1) + '/' + str(DATEPART(Day, tblMyEventTableName.TaskDateTime)) + '/'...
1
3623
by: Num | last post by:
Hi all, I have to convert a J2EE date as a long ("Millis") in a .NET date as a long ("Ticks") In Java, currentTimeMillis, is the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC. In .NET, DateTime.Ticks is the 100-nanosecond intervals that have
2
16204
by: Phil Stanton | last post by:
When designing a new form or report, the Default ForeColor is often something like -2147483640 which is the colour of Windows text (possibly black) and the default backColor is -2147483643 (possibly white) Can anyone tell me how to convert these colours to either RGB colours or the Long number used by Access. Black is 0 and White is 16777215 ...
9
90333
by: FalkoG | last post by:
Hello colleague I want to convert a floating number for example 5236.9856982 to a hexadecimal number. I'd tried several things but my problem is to convert the position after decimal point. I searched numberless websites but nothing could answer my question. I would be pleased to get some help. Regards
12
13900
by: Emi Lu | last post by:
Hello all, I have a question about "date" & "timestamp" types in PostgreSQL. I want to setup the default value '0000-00-00' and "0000-00-00 00:00:00" for them. However, it seems that PostgreSQL does not support it. Could someone helps me please? The example table: T1 (col1 varchar(7) not null,
17
71429
by: Terry Jolly | last post by:
New to C# ---- How do I convert a Date to int? In VB6: Dim lDate as long lDate = CLng(Date) In C#
7
19216
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006 00000000000000001111111111111111 11111111111111111111111111111111 00000000000000000000000000000000 11111111111111110000000000000000
7
7703
by: axapta | last post by:
Hi, I have the following however the date is returned in the long format. I want to display as dd/mm/yyyy. If Not drReader.Item("offerstatusdate") Is DBNull.Value Then txtOfferStatusDate.Text = drReader.Item("offerstatusdate") End If Regards
0
8610
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.