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#.
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.
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.
CultureInfo provider = CultureInfo.InvariantCulture;
string dateString = "08082010";
string format = "MMddyyyy";
DateTime result = DateTime.ParseExact(dateString, format, provider);
This will work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With