Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 20th, 2004, 03:17 PM
Registered User
 
Join Date: Apr 2004
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Convert String to DateTime

Hello,

I am trying to convert a string representation of a date in the format of yyyymmdd to a DateTime.

I'm trying to use inbuilt methods rather than write something custom.

Am I missing something very easy here (quite possible)?

Appreciate any replies.
Reply With Quote
  #2 (permalink)  
Old April 21st, 2004, 03:27 PM
Registered User
 
Join Date: Apr 2004
Location: Silver Spring, MD, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Simon --

Here is one way to do it:

            DateTime dt;
            string InputDateString = "20040421";

            int theYear = System.Convert.ToInt32(InputDateString.Substring(0 ,4));
            int theMonth = System.Convert.ToInt32(InputDateString.Substring(4 ,2));
            int theDay = System.Convert.ToInt32(InputDateString.Substring(6 ,2));


            dt = new DateTime(theYear, theMonth, theDay);

            Console.WriteLine(dt.Date);
            Console.Write(dt.Month+" "+dt.Day+", "+dt.Year);
            Console.ReadLine();

HTH,

Will
Reply With Quote
  #3 (permalink)  
Old April 21st, 2004, 03:42 PM
Registered User
 
Join Date: Apr 2004
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry, should have replied to myself earlier. My original code was failing because my time string was 95900 (meaning 9am and 59mins). I couldn't figure out why the DateTimeFormat below wasn't working. Have since resorted to appending a 0 on the front of any single digit hour - and then my code works as follows:

String date = "20041414 095900";
String DateTimeFormat = "yyyyMMdd Hmmss";
DateTime.ParseExact(date, DateTimeFormat, DateTimeFormatInfo.InvariantInfo,
DateTimeStyles.AllowLeadingWhite|DateTimeStyles.Al lowTrailingWhite);


Thanks for the response.

Reply With Quote
  #4 (permalink)  
Old October 11th, 2014, 02:24 AM
Registered User
Points: 6, Level: 1
Points: 6, Level: 1 Points: 6, Level: 1 Points: 6, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2014
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ParseExact method will allow you to specify the exact format of your date string to use for parsing. It is good to use this if your string is always in the same format.

string iString = "2005-05-05 22:12 PM";
DateTime oDate = DateTime.ParseExact(iString, "yyyy-MM-dd HH:mm tt", System.Globalization.CultureInfo.InvariantCulture) ;


The CultureInfo.InvariantCulture property is neither a neutral nor a specific culture. It is a third type of culture that is culture-insensitive. It is associated with the English language but not with a country or region.

Full Source:..C# String To DateTime

Eldo

Quote:
Originally Posted by SimoNWellborne View Post
Hello,

I am trying to convert a string representation of a date in the format of yyyymmdd to a DateTime.

I'm trying to use inbuilt methods rather than write something custom.

Am I missing something very easy here (quite possible)?

Appreciate any replies.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
convert string to datetime in VB.net lisabb ASP.NET 2.0 Basics 3 June 19th, 2007 06:52 AM
How to convert a string in DateTime elena SQL Language 6 March 29th, 2006 06:59 PM
Convert string to datetime nkovacevic General .NET 4 April 5th, 2005 11:57 AM
Convert DateTime rgerald SQL Server 2000 3 October 5th, 2004 09:00 PM
How to convert a string in DateTime elena ADO.NET 3 July 24th, 2003 04:31 PM



All times are GMT -4. The time now is 04:36 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.