You must Sign In to post a response.
  • Category: .NET

    How to convert December-2017 to 201712

    i have date format like December-2017 i want to convert 201712 please helm me to conversion how to do that .
  • #769528
    Hi Chanti,

    I am not sure at where you want this output, I have prepared a logic in SQL. You can convert the same into a function and use it

    DECLARE @InputDate NVARCHAR(50) = 'December-2017'
    DECLARE @Index INT = CHARINDEX('-',@InputDate)
    DECLARE @MonthName NVARCHAR(20)= UPPER(SUBSTRING(@InputDate,0,@Index))
    DECLARE @YearDate NVARCHAR(20) = SUBSTRING(@InputDate,@Index + 1,(LEN(@InputDate) - @Index) + 1)

    SELECT @YearDate+(CASE WHEN @MonthName = 'JANUARY' THEN '01'
    WHEN @MonthName = 'FEBRUARY' THEN '02'
    WHEN @MonthName = 'MARCH' THEN '03'
    WHEN @MonthName = 'APRIL' THEN '04'
    WHEN @MonthName = 'MAY' THEN '05'
    WHEN @MonthName = 'JUNE' THEN '06'
    WHEN @MonthName = 'JULY' THEN '07'
    WHEN @MonthName = 'AUGUST' THEN '08'
    WHEN @MonthName = 'SEPTEMBER' THEN '09'
    WHEN @MonthName = 'OCTOBER' THEN '10'
    WHEN @MonthName = 'NOVEMBER' THEN '11'
    WHEN @MonthName = 'DECEMBER' THEN '12'
    ELSE '' END)

    Let me know if you have any questions

    V.M. Damodharan
    "Your talent will be worthless, when you have fear and tension."

  • #769629
    Hi Chanti,

    I don't know where you want the output. So this is my answer in C#

    string[] array = Convert.ToString("December-2017").Split('-');
    Label1.Text = array[1] + DateTime.ParseExact(array[0].ToString().SubString(0,3), "MMM", System.Globalization.CultureInfo.InvariantCulture).Month.ToString("00");

    Hope this will help you.

    Nirav Lalan
    DNS Gold Member
    "If you can dream it, you can do it."

  • #769653
    Basically when we work with .net we used to format the date value using Format function. so , below is the straight code to convert actual date value to expected format


    Hope this will work and get you the expected format of value.

    B.Ramana Reddy

  • Sign In to post your comments