## 7 replies

Last post Nov 01, 2016 11:18 PM by PaulTheSmith

Member

27 Points

53 Posts

### Convert Decimal yyyymm to Date

How to convert Decimal value =  201611  to date 11/1/2016

http://e-webtemplates.com/

Contributor

3290 Points

867 Posts

### Re: Convert Decimal yyyymm to Date

```CultureInfo provider = CultureInfo.InvariantCulture;
string dateString = "20161101";
string format = "yyyyMMdd";
DateTime result = DateTime.ParseExact(dateString, format, provider);```

Refer this fiddle

Regards,

Senthil Kumar Sundaram
• ### Mikesdotnett...

All-Star

184653 Points

26697 Posts

Moderator

MVP

### Re: Convert Decimal yyyymm to Date

There is no conversion between decimals and dates, but you can convert the decimal to a string and then use string manipulation:

```var input = 201611d.ToString();
var date = new DateTime(Convert.ToInt32(input.Substring(0, 4)), Convert.ToInt32(input.Substring(4,2)), 1);```

• ### Mikesdotnett...

All-Star

184653 Points

26697 Posts

Moderator

MVP

### Re: Convert Decimal yyyymm to Date

#### senthilwaits

string dateString = "20161101";
That's not what the OP provided as a decimal value.

Contributor

3290 Points

867 Posts

### Re: Convert Decimal yyyymm to Date

Thanks Mike for the correction.  I missed it.

Kanmai,

Here is an alternate way

```CultureInfo provider = CultureInfo.InvariantCulture;
string dateString = 201611d.ToString();
string format = "yyyyMM";
DateTime result = DateTime.ParseExact(dateString, format, provider);```

I corrected the fiddle as well

Regards,

Senthil Kumar Sundaram

Participant

1060 Points

346 Posts

### Re: Convert Decimal yyyymm to Date

https://dotnetfiddle.net/KP8q7S

Thanks, Sumit.

Member

710 Points

352 Posts

### Re: Convert Decimal yyyymm to Date

Some slight variations

var November = DateTime.ParseExact(\$"{value}", "yyyyM", null);

or

var November = new DateTime((int)value/100, (int)value%100, 1);

(as an aside - this is a bad time of year to be doing this work.  Sometimes your code will work for a couple of months and then the value 20171 turns up when you had assumed that 201701 would be passed, sigh.  In that case my first version will work but the second will not.  You should check whichever way you go.)

Member

710 Points

352 Posts