none
Convert a number to date format mm/dd/yyyy using c#

    Question

  • I'm struggling with converting a numeric field into a date format of mm/dd/yyyy with c#.  I created a search data screen on a file with lightswitch 2012 since the data is old and we don't want to change it. The legacy system was written in SYNON programming language and the dates are stored in CYYMMDD format. For example if the number is 0920101 the 0 is interpreted as the 20th century, hence that date converted would be 01/01/1992.  If the date is 1120101 the 1 is interpreted as the 21st century, hence that date would be 01/01/2013. I've created a calculated field of date format but am not having much luck converting the existing date.  Thanks for your help.

    Vern Miller

    Sunday, February 03, 2013 1:54 PM

Answers

  • So, I guess to answer my own question... you've attached to an existing data source (the SQL 2012 database) and the property is an Integer type. Correct?

    Computationally, it's easiest if it is an integer rather than a string:

    private static DateTime? ToDateTime(int synonDate)
    {
        int day = synonDate % 100;
        int dateWithoutDay = synonDate / 100;
        int month = dateWithoutDay % 100;
        int dateWithoutDayAndMonth = dateWithoutDay / 100;
        int year = dateWithoutDayAndMonth % 100;
        int century = dateWithoutDayAndMonth / 100;
    
        if (0 == day || 0 == month)
            return null;
        else
            return new DateTime((19 + century) * 100 + year, month, day);
    }
    I also think your example of 1120101 should result as 1/1/2012 and not 1/12013 if I understand the algorithm correctly.

    Justin Anderson, LightSwitch Development Team

    Tuesday, February 05, 2013 7:39 AM
    Moderator

All replies

  • You say 'numeric' field but show a leading zero '0920101' so is it really text/string?

    Dave Baker | Xpert360 blog | twitter : @xpert360 Opinions are my own. Please mark as answer if this helps solve your problem.

    Monday, February 04, 2013 2:51 AM
  • It is a 7 digit numeric.  Thanks.

    Vern Miller

    Monday, February 04, 2013 1:56 PM
  • Have you attached to an existing data source or are you trying to recreate your application data in LightSwitch from scratch? If you have attached, what is the type of the field that contains this "7 digit numeric" (there is no Numeric type in LightSwitch)? If it is a new schema that you are trying to create in LightSwitch, the type of the field should probably be String.


    Justin Anderson, LightSwitch Development Team

    Tuesday, February 05, 2013 12:18 AM
    Moderator
  • I brought the application data into a SQL 2012 database.  I could redo my script and bring in the numeric data as char if that appears to be the only way to handle the problem.  Thanks for your reply.

    Vern Miller

    Tuesday, February 05, 2013 12:51 AM
  • So, I guess to answer my own question... you've attached to an existing data source (the SQL 2012 database) and the property is an Integer type. Correct?

    Computationally, it's easiest if it is an integer rather than a string:

    private static DateTime? ToDateTime(int synonDate)
    {
        int day = synonDate % 100;
        int dateWithoutDay = synonDate / 100;
        int month = dateWithoutDay % 100;
        int dateWithoutDayAndMonth = dateWithoutDay / 100;
        int year = dateWithoutDayAndMonth % 100;
        int century = dateWithoutDayAndMonth / 100;
    
        if (0 == day || 0 == month)
            return null;
        else
            return new DateTime((19 + century) * 100 + year, month, day);
    }
    I also think your example of 1120101 should result as 1/1/2012 and not 1/12013 if I understand the algorithm correctly.

    Justin Anderson, LightSwitch Development Team

    Tuesday, February 05, 2013 7:39 AM
    Moderator
  • Thanks for the reply and I will give it a try.  Yes, you are correct, I was in error on the 1120101 date.

    Vern Miller

    Tuesday, February 05, 2013 2:18 PM