Solved

what is a safe method of formatting dates to such as dd/mm/yyyy in C#

Posted on 2009-07-01
5
255 Views
Last Modified: 2013-12-17
I am relatively new to C#. been googling about this but see no good way of doing what I want.
i want to convert any type of date to dd/mm/yyyy ( or dd-mm-yyyy for that matter ).
The string I want to convert can be in ay valid date format such as
MM/dd/YYYY  
MM.dd.yyyy
dd/mm/yy

Thanks very much.
0
Comment
Question by:indikad
5 Comments
 
LVL 39

Accepted Solution

by:
abel earned 125 total points
ID: 24754233
Before you convert it, you need to know what format you use for input. Often, when you "just" use DateTime.Parse it will go correct (use .TryParse to find out whether it is parsable at all and prevent an error). You cannot reasonably expect to know beforehand that a date like 03/05/2006 is 03 May or 05 March. If you cannot know that, the computer cannot either.

Can you update with how we can determine what format to use from the three possibilities above? How do you define "any" date format, because there are literally thousands and thousands of date formats in the world....
0
 
LVL 2

Assisted Solution

by:softbee
softbee earned 125 total points
ID: 24754271
Date and Time representations are - in all MS language versions - coupled to the 'LOCALE' that the computer is in. In programs written for the international market it is a given that the date/time shown by a program will be that which the person using the machine is used to. Taking proper care with the 'LOCALE' makes this mostly fully automatic.
The keywords Locale and CultureInfo in MSDN should lead you in the right direction.
0
 
LVL 3

Assisted Solution

by:Dov_Landau
Dov_Landau earned 125 total points
ID: 24754417

If you use the CultureInfo then you can not say that every Date string is appropriate for the conversion, because only valid dates - according to the specific culture - are acceptable.
If you do not use the culture info - then you can not parse every valid string date, because you don't know how to distignuish between dates.
For example: if you get 11/12/2008. you don't know whether the 11 means month and 12 means day, or vice versa.
0
 
LVL 8

Assisted Solution

by:dericstone
dericstone earned 125 total points
ID: 24756471
Since you say you are relatively new to C#, you might not realize that most types have a ToString() method that will create a string representation of the object. So once you have parsed the input string into a DateTime object (as abel explained above) you can just call ToString() to get the newly formatted string. I think there are more options for formatting the string, but that should get you started.
0
 

Author Closing Comment

by:indikad
ID: 31598759
Thank you for all - now I have a good starting point and some very good directions.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now