473,748 Members | 2,426 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 6735
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
9299
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
7287
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
3627
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
16217
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
90417
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
13912
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
71450
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
19225
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
7711
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
8984
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
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
9363
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
9312
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
9238
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...
0
8237
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
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
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2206
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.