473,322 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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(datetime, Convert(int, '38875')-2))

but was wondering if there was a C# way?

Cheers,

OO

Nov 17 '05 #1
6 6713
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(datetime, 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.com

"Cowboy (Gregory A. Beamer) - MVP" <No************@comcast.netNoSpamM> wrote
in message news:E4**********************************@microsof t.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(datetime, 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*********************@f14g2000cwb.googlegro ups.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.AddDays(valueFromDatabase);

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(datetime, 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.netNoSpamM> wrote
in message news:E4**********************************@microsof t.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(datetime, 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 InvalidCastException 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
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...
19
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....
1
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...
2
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...
9
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...
12
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...
17
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
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...
7
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 =...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.