473,406 Members | 2,847 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,406 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 6717
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 =...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.