Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

Format string as short date

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
moondaddy - 11 Feb 2008 17:57 GMT
using c#3.5, how do I take a string which contains a data, and format it as
a short data format like this:

"2/5/2008 1:17:11 AM"

and format to:

"02/05/2008"

Thanks.

Signature

moondaddy@newsgroup.nospam

Peter Duniho - 11 Feb 2008 18:08 GMT
> using c#3.5, how do I take a string which contains a data, and format it  
> as
[quoted text clipped - 5 lines]
>
> "02/05/2008"

Well, one way would be to use the DateTime.TryParse() method to convert  
the string to a DateTime object, and then use the  
DateTime.ToShortDateString() to reformat the data into the the string you  
want.

Pete
Cor Ligthert[MVP] - 11 Feb 2008 18:17 GMT
MoonDaddy,

First check the globalization settings are US then just

\\\

var date = DateTime.Parse("5/2/2008 1:17:11").ToShortDateString();

///

I have done it in my globalisation settings.

Cor
Nicholas Paldino [.NET/C# MVP] - 11 Feb 2008 18:20 GMT
Well, first you would parse the original string into a DateTime
instance.  I would call TryParse, passing the general date time pattern "g"
(assuming that your culture is "en-us").

   Because the generat date time pattern is dependent on the current
culture information to determine the format of the general date/time
pattern, if you want to use an exact pattern, you would use:

d/M/yyyy h:mm:ss tt

   Of course, even the above is subject to culture info, in that the AM/PM
designator is specific to the DateTimeFormatInfo on the current culture.
You would probably want to make sure that the invariant culture is used when
parsing.

   Then, once you have the DateTime instance, you can call ToString on it,
passing a format of:

dd/MM/yyyy

   For your output.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> using c#3.5, how do I take a string which contains a data, and format it
> as a short data format like this:
[quoted text clipped - 6 lines]
>
> Thanks.
Ignacio Machin ( .NET/ C# MVP ) - 11 Feb 2008 18:27 GMT
Hi,

You have to convert the string to DateTime and then use
DateTime.ToShortDateString();

Take a lookat DateTime.TryParse or Convert.ToDateTime

Signature

Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.

> using c#3.5, how do I take a string which contains a data, and format it
> as a short data format like this:
[quoted text clipped - 6 lines]
>
> Thanks.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.