Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 20th, 2004, 03:17 PM
Registered User
 
Join Date: Apr 2004
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.
 
Old April 21st, 2004, 03:27 PM
Registered User
 
Join Date: Apr 2004
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
 
Old April 21st, 2004, 03:42 PM
Registered User
 
Join Date: Apr 2004
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.

 
Old October 11th, 2014, 02:24 AM
Registered User
 
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.





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.