Link to home
Start Free TrialLog in
Avatar of Boon01
Boon01

asked on

Best way to handle date on different PCs...

I am running VB.Net writing/reading to an Access 2003 database.

Alright, being new to programming I have come to the realization that I dont like Dates in any form or fashion. I thought I had it all figured out and was validating/saving/retrieving date just fine. And then I installed my compiled program on a clean PC and found a huge problem.

On my PC (and my test PC) I have my short date set to MM/DD/YYYY. And life is good and everything flows. We installed this on a different PC that had the short date set to M/D/YY and everything went down hill really fast. With error messages, invalid format errors etc.

I am saving my data with a masked text box and validating that it is in MM/DD/YYYY format when I save. But if someone has a PC with the date setting that is different then it reads it fine but it displays it in M/D/YY format. So my mask/validation has a real problem with this.

Can anyone tell me the best way to handle the different date formats from one PC to another? I dont really want to tell everyone that they have to change their date setting to conform to what my program is looking for. I am sure that I am not the first person to come across this.

Thanks for any input/help that you can give.
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

The most important thing when working with dates is consistency; use explicit formatting at all times

Real World Best Practices for Handling Dates
http://www.codeproject.com/csharp/datetime_best_practices.asp
Avatar of Boon01
Boon01

ASKER

Thanks for the reply...
To be more direct...

When I debug, this line of code is where my problem is...

If ShowPatient.IsNull("PatDOB") Then
txtDOB.Text = ""
Else : txtDOB.Text = CDate(ShowPatient.PatDOB)

ON the Else line....I can see that the ShowPaitent.PatDOB value is in the MM/DD/YYYY format (coming right out of my database), but when I process this line of code the txtDOB value ends up being M/D/YY which is the wrong format that I am looking for. Does this help clear up what I am looking for?
 
ASKER CERTIFIED SOLUTION
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>
I can see that the ShowPaitent.PatDOB value is in the MM/DD/YYYY format (coming right out of my database)
<<

What datatype is coming out of your database?  If it's a Date (or DateTime) it doesn't come out of the database in any format.  It comes out as a DateTime VALUE.  Such a value (a series of bits) would mean nothing to us mere humans so every time we ask any system to tell us what the value is, it formats it for the purpose of display.  You can tell the various systems what format you would prefer for that display: either in general terms, as some sort of default setting, or on a specific occasion.  But just because (for instance) your Access database is formatting it DD/MM/YYYY when you ask Access to see what it is, that has no bearing on the date VALUE that comes out of the database.  And how the PC on which the app formats it is - as you've discovered - dependent on the settings on that PC, although as Dhaest as illustrated, you can override those default settings with specific formatting.

But I ask the question because it may be that the datatype that is coming out of your database is not a Date (or DateTime) datatype.  Instead it is a String looking, for example, like "01/01/2006".  That that may be the case is indicated by your reference to "ShowPaitent.PatDOB value is in the MM/DD/YYYY format (coming right out of my database)" and by the fact that before you use the value you put it in CDate.  But, if it is indeed a string looking like that, all you would need would be

txtDOB.Text = ShowPatient.PatDOB

Roger
Avatar of Boon01

ASKER

Hey guys, thanks for your suggestions.

Daest: when I use the ToString.("MM/DD/YYY") I end up with a value of (for example) "07/  /   " instead of "07/17/1960".

Sancler: Yes the field is set in my database as a data type of Date/Time with a format of Short Date.
Avatar of Boon01

ASKER

Oh and when I use just txtDOB.Text = ShowPatient.PatDOB then I get 71/16/0 instead of 07/17/1960.
Can you show how you get your data from the database and how you store it into PatDOB
Avatar of Boon01

ASKER

This is how I store/save this field (this is a Masked Textbox set with a mask of 00/00/0000 and set as ShortDate)...
 Dim EditRow As AnesDataSet.PatientInfoRow
 EditRow = AnesDataSet.PatientInfo.FindByPatMRNo(MR)

'Update DOB text
            If txtDOB.Text = "  /  /" Then
                EditRow.PatDOB = Nothing
            Else
                EditRow.PatDOB = txtDOB.Text
            End If

This is how I show/get the infomation...
 Dim ShowPatient As AnesDataSet.PatientInfoRow
 PatientInfoTableAdapter1.Fill(AnesDataSet.PatientInfo)
 ShowPatient = AnesDataSet.PatientInfo.FindByPatMRNo(MR)

'DOB text
        If ShowPatient.IsNull("PatDOB") Then
            txtDOB.Text = ""
        Else : txtDOB.Text = (ShowPatient.PatDOB).ToString("MM/DD/YYYY")
            'Else : txtDOB.Text = ShowPatient.PatDOB
            'Else : txtDOB.Text = CDATE(ShowPatient.PatDOB)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I always write the date as a string in the database,
best way is YYYYMMDD.

I've had some troubles before with dates on different machines wiht USA format or European format etc.

I discided to use Strings.
Avatar of Boon01

ASKER

Yup that was it. Thanks very much to both Dhaest and Sancler for your help!