?
Solved

Best way to handle date on different PCs...

Posted on 2007-07-25
11
Medium Priority
?
185 Views
Last Modified: 2013-11-26
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.
0
Comment
Question by:Boon01
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 19565262
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
0
 
LVL 1

Author Comment

by:Boon01
ID: 19565594
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?
 
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 280 total points
ID: 19565632
txtDOB.Text = CDate(ShowPatient.PatDOB).tostring("MM/DD/YYYY")


http://articles.techrepublic.com.com/5100-3513-6106789.html
0
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!

 
LVL 34

Expert Comment

by:Sancler
ID: 19565806
>>
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
0
 
LVL 1

Author Comment

by:Boon01
ID: 19565996
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.
0
 
LVL 1

Author Comment

by:Boon01
ID: 19566021
Oh and when I use just txtDOB.Text = ShowPatient.PatDOB then I get 71/16/0 instead of 07/17/1960.
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 19567148
Can you show how you get your data from the database and how you store it into PatDOB
0
 
LVL 1

Author Comment

by:Boon01
ID: 19567252
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)
0
 
LVL 34

Assisted Solution

by:Sancler
Sancler earned 220 total points
ID: 19568520
The format string for

txtDOB.Text = (ShowPatient.PatDOB).ToString("MM/DD/YYYY")

should be "MM/dd/yyyy" not "MM/DD/YYYY"

Roger
0
 
LVL 2

Expert Comment

by:TaeSoft
ID: 19568547
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.
0
 
LVL 1

Author Comment

by:Boon01
ID: 19569367
Yup that was it. Thanks very much to both Dhaest and Sancler for your help!
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

850 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