Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date formatting yyyymmdd to yyyy-mm-dd

Tags:

c#

.net

datetime

I'm trying to convert a date in yyyymmdd format to yyyy-mm-dd with the following code:

tdrDate = DateTime.ParseExact(dateString, "yyyymmdd", null).ToString("yyyy-MM-dd");

This works the only problem is that when I have a date such as this "20070205" I get back "2007-01-05". I don't know why this is happening, any help is appreciated.

like image 257
kingrichard2005 Avatar asked Nov 22 '10 22:11

kingrichard2005


People also ask

How do I change the date format from Yyyymmdd to yyyy mm dd in SQL?

Convert Char 'yyyymmdd' back to Date data types in SQL Server. Now, convert the Character format 'yyyymmdd' to a Date and DateTime data type using CAST and CONVERT. --A. Cast and Convert datatype DATE: SELECT [CharDate], CAST([CharDate] AS DATE) as 'Date-CAST', CONVERT(DATE,[CharDate]) as 'Date-CONVERT' FROM [dbo].

How do I format a date in Yyyymmdd?

To format a date as YYYYMMDD : Use the getFullYear() , getMonth() and getDate() methods to get the year, month and day of the date. Add a leading zero to the day and month digits if the value is less than 10 . Add the results to an array and join them without a separator.

How do I change a date format from YYYY to MM DD in Excel?

First, pick the cells that contain dates, then right-click and select Format Cells. Select Custom in the Number Tab, then type 'dd-mmm-yyyy' in the Type text box, then click okay. It will format the dates you specify.


2 Answers

tdrDate = DateTime.ParseExact(dateString, "yyyyMMdd", null).ToString("yyyy-MM-dd");

You need MM, not mm. mm is for minutes.

like image 200
GendoIkari Avatar answered Oct 15 '22 16:10

GendoIkari


It should be:

DateTime.ParseExact(dateString, "yyyyMMdd", null).ToString("yyyy-MM-dd");

Capital 'MM' in the first date format string.

like image 30
Mark Bell Avatar answered Oct 15 '22 16:10

Mark Bell