Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

what is the default value of a date variable?

Posted on 2006-06-03
8
Medium Priority
?
1,093 Views
Last Modified: 2012-06-27
if you declare a date varible using Dim - what is in it?

an object reference is initially set to Nothing - what about a date?

how would i test if a date varible is uninitialised?
0
Comment
Question by:kenshaw
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 10

Expert Comment

by:sakuya_su
ID: 16826529
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vbconLifetime.asp

according to MSDN it is set to the mid night of the year 1, month 1 and day 1.

in PHP I think its set to like 196* somehying, but I think we can trust MSDN.

also you can just try printing out the variable after you declare it to see what it contains
0
 
LVL 13

Expert Comment

by:iHadi
ID: 16826530
Hi
The initialize value of the date depends on your system configurations, mine is 30/12/1899
To demonstrate whates happening try the following code

Private Sub Command1_Click()
Dim tmpDate As Date
Dim tmpStr As String


tmpStr = "Hour is: " & Hour(tmpDate) & vbCrLf
tmpStr = tmpStr & "Minute is: " & Minute(tmpDate) & vbCrLf
tmpStr = tmpStr & "Second is: " & Second(tmpDate) & vbCrLf
tmpStr = tmpStr & "Day is: " & Day(tmpDate) & vbCrLf
tmpStr = tmpStr & "Month is: " & Month(tmpDate) & vbCrLf
tmpStr = tmpStr & "Year is: " & Year(tmpDate) & vbCrLf

Print tmpStr

' to check if the date is initializes or not
Print CDbl(tmpDate) ' not initialized returns 0

tmpDate = Now
Print CDbl(tmpDate) ' initialized returns a value
End Sub
0
 
LVL 14

Accepted Solution

by:
MilanKM earned 2000 total points
ID: 16826532
In VB6 default date of a Date object contains both the date and time at which the object was instantiated.

A Date variable value specifies both date and time. The default value for Date is 00:00:00 30 December 1899. If you specify a date but not a time, the default time of 00:00:00 is used. If you specify a time, but not a date, the default date of 30 December 1899 is used. Here the range of date in VB6 is 1 January 100 to 31 December 9999.

Thanks
MilanKM
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 14

Expert Comment

by:MilanKM
ID: 16826540
however to check simply do the followings

Dim dt As Date

Private Sub Form_Load()
    MsgBox dt
End Sub
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16827154
The correct answer is contains 30 Dec 1899 @ 12:00:00 AM.
You cannot tell if it has been initialized or not.



From the help file:

    Date variables are stored as IEEE 64-bit (8-byte) floating-point numbers that represent dates ranging from 1 January 100 to 31 December 9999 and times from 0:00:00 to 23:59:59.

    When othernumeric types are converted to Date, values to the left of the decimal represent date information while values to the right of the decimal represent time. Midnight is 0 and midday is 0.5. Negative whole numbers represent dates before 30 December 1899.



So you can't rely on CDbl(tmpDate) = 0 to indicate an uninitialized Date.  A value of zero simply means it represents 30 December 1899 at Midnight as demonstrated by this code:

    Private Sub Command1_Click()
        Dim dt As Date
        Debug.Print "Default Date:"
        Debug.Print Format(dt, "d mmm yyyy") & " @ " & Format(dt, "ttttt")
        Debug.Print "CDbl() = " & CDbl(dt)

        dt = DateSerial(1899, 12, 30)
        Debug.Print "Initialized Date:"
        Debug.Print Format(dt, "d mmm yyyy") & " @ " & Format(dt, "ttttt")
        Debug.Print "CDbl() = " & CDbl(dt)
   End Sub

Which gives the following output:

    Default Date:
    30 Dec 1899 @ 12:00:00 AM
    CDbl() = 0
    Initialized Date:
    30 Dec 1899 @ 12:00:00 AM
    CDbl() = 0

Since internally a date is just an "(8-byte) floating-point number" it doesn't have an uninitialized state.  It just defaults to zero.  Microsoft decided to use 30 Dec 1899 @ 12:00:00 AM as the "base date".  Negative numbers represent dates before this and positive numbers represent dates after this.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16827174
sakuya_su, the documentation you linked is for VB.Net:

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dt As DateTime
        Debug.WriteLine(dt.ToString)
    End Sub

Which gives us:

    1/1/0001 12:00:00 AM

Just like VB6, a VB.Net DateTime does not have an uninitialized state as it is also internally represented as a number (64 bits).
0
 
LVL 13

Expert Comment

by:iHadi
ID: 16828210
Idle_Mind you are right about the initialization issue but we're assuming that an application developed doesn't need a date from the 18th century and its a way round. If the application needs to deal with this particular date then I think kenshaw () should look for another way to detect if the date has been initialized. Maybe using an alternative type instead of Date that can represent dates like string.
0
 
LVL 9

Expert Comment

by:dancebert
ID: 16829350
Private MySub
    Dim d as date
End Sub

Put a breakpoint on the End Sub line and look at the value of d in the locals window.  It's "12:00:00am", with no date.  
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question