vb.net date serial

Zeus2009
Zeus2009 used Ask the Experts™
on
Hi all,

I am using vb2005 CF.net 2, i have come across a problem with dates in different regions.

can someone provide an example of how i can format a date to MM/dd/yyyy no matter what region the mobile device is set to.

Thanks
Zeus
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Use the ToString() method and specify your desired format:

    Label1.Text = someDate.ToString("MM/dd/yyyy")

Author

Commented:
Hi thanks for the reply,
the issue is i already have the above in my code but when casting from a string to date and it is in a different region to that of the date in the string i get an invalid cast exception.

any ideas

Thanks
Zeus
Most Valuable Expert 2012
Top Expert 2014

Commented:
Use the Convert function
Convert(Date, "date", 103)
where 103 is one of the date formats (the one you need).
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
To parse it back to a Date you need to specify the correct CultureInfo:

    Dim strDate As String = "07/17/2009"
    Dim dt As DateTime = DateTime.Parse(strDate, New System.Globalization.CultureInfo("en-US", False))

Author

Commented:
is there no global way of achieving the convert back to date without knowing the culture?

Thanks
Zeus
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Not that I'm aware of...

Author

Commented:
ok well i am using dates to store files and i load a list of files into a combobox whos names are dates with which the user can choose from.

so i really need to all dates both string and cast to date to be in the same format a more clear explination would be great.

Zeus
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
ok...what part do you need help with here?

Author

Commented:
Hi,
ok, the main objective is being able to sort a list of dates in a combobox, the combobox is populated from files stored with a date as there name i.e 07-17-2009, when i get an array of file names i then need to convert them to dates so that i can sort them using a compare function, but the problem being is the application can be any where in the world i.e multiple reginal settings.

further on in the application when the user selects an entry from the combox it will go and get the file pertaining to the item selected.

hope this helps
Most Valuable Expert 2012
Top Expert 2014

Commented:
I think you should use the datatable storing filenames in one column and converted dates to another, corresponding, column. This way, you do not need to convert the dates back to strings. Bind the date column to the combobox and use the string column as the valuefield.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
Can you give examples of the filenames?...are they JUST the date with an extension, or is there other stuff in the name?

Does your array of filenames have FULL paths or just the file names?  

Also, if your app is CREATING the files in the other cultures as well then be sure to use the "en-US" culture when you use DateTime.ToString().

Author

Commented:
Hi again,

Thanks for the feedback the piece of code below has done the trick as both idel mind and code cruiser tried to help i will split the points between you both.

Thanks

Zeus

    Dim strDate As String = "07/17/2009"
    Dim dt As DateTime = DateTime.Parse(strDate, New System.Globalization.CultureInfo("en-US", False))

Author

Commented:
thanks guys
Most Valuable Expert 2012
Top Expert 2014

Commented:
>as both idel mind and code cruiser tried to help i will split the points between you both.
You selected both comments from idle_mind!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial