locked
Convert Integer to DateTime RRS feed

  • Question

  • I am new to programming and lightswitch. This is my first serious attempt. I have an SQL Server Express database table as shown below with the following fields. The nDateTime field represents a date and time information coming from a fingerprint device.  I have connected to this table in Lightswitch and need to recreate the date and time information. however the type of nDateTime is INT  and can't figure how to go about it.  In Excel, i can convert it to the correct date using the following fomula and formating as Date   =(DATEVALUE("1/1/1970")+K8/(24*60*60))

    Thanks a million for your assistance in advance

    nEventLogIdn   nDateTime         nReaderIdN

    1                    1341512409              54732
    2                    1341512427              54732
    6                    1341831062              54732
    7                    1341841023              54732
    8                    1341841025              54732
    9                    1341841053              54732
    10                    1341841676              54732
                       

               

    Wednesday, October 31, 2012 10:07 AM

Answers

  • i could not get the correct dates with above. However, this did the trick. Thanks a million

    result = DateTime.Parse("January 1 1970 12:00:00 am").AddSeconds(Me.nDateTime)

    • Marked as answer by wofatampofo Friday, November 2, 2012 7:41 PM
    Friday, November 2, 2012 7:40 PM

All replies

  • Depending on how many rows you'll have, you could add a Computed Property to the table.

    1. Open the table in the table designer

    2. Click on "Computed Property":

    3. Click on "Edit Method:

    4. Then you can enter your conversion logic in the method:

    Private Sub Property1_Compute(ByRef result As String)
        ' Set result to the desired field value
        result = new Date(1970, 1, 1).AddDays(Me.nDateTime/24*60*60)
    End Sub


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    Wednesday, October 31, 2012 2:37 PM
    Moderator
  • Thanks for your quick response. i tried it but i get the following error:

    System.ArgumentOutOfRangeException was unhandled by user code
      Message=Value to add was out of range.
    Parameter name: value
      StackTrace:
           at System.DateTime.Add(Double value, Int32 scale)
           at System.DateTime.AddDays(Double value)
           at LightSwitchApplication.EVENTLOG.EmpDateTime_Compute(String& result)
           at LightSwitchApplication.EVENTLOG.DetailsClass.PropertySetProperties._EmpDateTime_ComputeValue(EVENTLOG e)
           at Microsoft.LightSwitch.Details.Framework.EntityComputedProperty`3.Entry.<>c__DisplayClass51.<>c__DisplayClass53.<.ctor>b__12()
           at Microsoft.LightSwitch.Utilities.Internal.UserCodeHelper.CallUserCode(Type sourceType, String methodName, String instance, String operation, ILoggingContext context, Action action, String additionalText, Func`1 getCompletedMessage, Boolean tryHandleException, Boolean swallowException, Exception& exception)
      InnerException:

    Thanks

    Wednesday, October 31, 2012 5:55 PM
  • I think the math is wrong due to order of operations. But you can avoid doing the math by just using the AddSeconds method instead of AddDays:

    new Date(1970, 1, 1).AddSeconds(Me.nDateTime)


    Justin Anderson, LightSwitch Development Team

    Wednesday, October 31, 2012 8:43 PM
    Moderator
  • Uggh, I totally missed the fact that the value represented "seconds".

    Nice catch, as usual, Justin.


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer" if a reply answers your question. Please click "Vote as Helpful" , if you find a reply helpful.
     
    By doing this you'll help others to find answers faster.

    Thursday, November 1, 2012 2:31 AM
    Moderator
  • i could not get the correct dates with above. However, this did the trick. Thanks a million

    result = DateTime.Parse("January 1 1970 12:00:00 am").AddSeconds(Me.nDateTime)

    • Marked as answer by wofatampofo Friday, November 2, 2012 7:41 PM
    Friday, November 2, 2012 7:40 PM
  • My way and your way return exactly the same values. But I wouldn't use DateTime.Parse because you may run into weird behaviors if your users are using a different culture that doesn't understand what "January 1 1970 12:00:00 am" is.


    Justin Anderson, LightSwitch Development Team

    Saturday, November 3, 2012 12:08 AM
    Moderator