skip to Main Content

I need your support to convert integer: 20160513225918

I wanted like : 2016-05-13 22:59:18

thank you.

2

Answers


  1. Important Point: First thing that your value does not fit in the integer datatype, because value gets overflow. Int accepts value within -2,147,483,648 to 2,147,483,647. Try to store that value in long

    When you try DateTime.ParseExact() it will throw an error input string was not recognized as a valid datetime

    So you can extract the each individual part using substring

    Like this

    long data = 20160513225918;
    int year = int.Parse(data.ToString().Substring(0, 4));
    int month = int.Parse(data.ToString().Substring(4, 2));
    int day = int.Parse(data.ToString().Substring(6, 2));
    int hour = int.Parse(data.ToString().Substring(8, 2));
    int minute = int.Parse(data.ToString().Substring(10, 2));
    int second = int.Parse(data.ToString().Substring(12, 2));
    
    //bellow line will get you "2016-05-13 22:59:18"
    var date = new DateTime(year, month, day, hour, minute, second).ToString("yyyy-MM-dd HH:mm:ss");
    
    1. The first parameter inside Substring is start index
    2. The second parameter inside Substring is length
    Login or Signup to reply.
  2. You can just use basic arithmetic for this, no string parsing necessary

    var value = 20160513225918;
    
    return new DateTime(
        (int)(value / 10000000000),
        (int)(value / 100000000 % 100),
        (int)(value / 1000000 % 100),
        (int)(value / 10000 % 100),
        (int)(value / 100 % 100),
        (int)(value % 100));
    

    dotnetfiddle

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search