Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting 8 digit number to DateTime Type

Tags:

c#

I want to convert 8 digit value to a DateTime object. How can I do this? For example, if a user enters 08082010 then it should convert it to 08/08/2010, using C#.

like image 391
Asim Sajjad Avatar asked Mar 14 '10 06:03

Asim Sajjad


People also ask

How do I change my 8 digit number to a date?

Select the numbers. On the Data tab of the ribbon, click Text to Columns. Click Next >, then Next > again. Under 'Column data format', select Date, then select YMD from the drop-down next to the Date option button.

How do you convert a string to a number to a date?

The DATEVALUE function in Excel converts a date in the text format to a serial number that Excel recognizes as a date. So, the formula to convert a text value to date is as simple as =DATEVALUE(A1) , where A1 is a cell with a date stored as a text string.


1 Answers

CultureInfo provider = CultureInfo.InvariantCulture;
string dateString = "08082010";
string format = "MMddyyyy";
DateTime result = DateTime.ParseExact(dateString, format, provider);

This will work.

like image 140
Jason Avatar answered Sep 28 '22 10:09

Jason