Forum Discussion

leandropoblet's avatar
leandropoblet
Frequent Contributor
11 years ago

Converting dates to utc

Hi,



I'm pulling a date from the application being tested and I know that date/time value is stored in the DB as a utc datetime.



How can I convert from a local time to utc using c# script?



I cound't find any function to do it.



Thanks,

Leandro

  • Hi Leandro, one option is to utilise the CLR Bridge and use the .Net System.DateTime Class, something like


     


    function dateToUTC() {

      var dt = dotNET["System"]["DateTime"]["Now"];


      try {


        dt = dt["Parse"]( "30/04/2013 08:41:00" );


        utc = aqConvert["StrToDateTime"]( dt["ToUniversalTime"]() );


        Log["Message"]( dt["ToString"]() );


        Log["Message"]( utc );


      } catch( ex ) {


        Log["Message"]( "Error Parsing DateTime" );


      }


    }



     


     


    Regards,


    Phil Baird

  • Hi Phil,



    here is the code that works for me:



    function DateToUTC(dateToBeConverted)

    {

      //  Converts any given datetime to utc

     

      var dt = dotNET["System"]["DateTime"]["Now"];

         

      dt = dt["Parse"]( dateToBeConverted );

      utc = aqConvert["StrToDateTime"]( dt["ToUniversalTime"]() );

         

      //Log["Message"]( utc );

         

      return( utc );

    }



    I don't really use try/catch since it 'hides" errors specially while developing.



    Cheers,

    Leandro



  • Philip_Baird's avatar
    Philip_Baird
    Community Expert

    Hi Leandro, one option is to utilise the CLR Bridge and use the .Net System.DateTime Class, something like


     


    function dateToUTC() {

      var dt = dotNET["System"]["DateTime"]["Now"];


      try {


        dt = dt["Parse"]( "30/04/2013 08:41:00" );


        utc = aqConvert["StrToDateTime"]( dt["ToUniversalTime"]() );


        Log["Message"]( dt["ToString"]() );


        Log["Message"]( utc );


      } catch( ex ) {


        Log["Message"]( "Error Parsing DateTime" );


      }


    }



     


     


    Regards,


    Phil Baird

  • Philip_Baird's avatar
    Philip_Baird
    Community Expert

    Hi Leandro, I tried using "2014-04-30 08:00:00" as a parameter and it worked ok for me.


     


    Can you place a break point on the line Log["Message"]( "Error Parsing DateTime" ); and inspect ex and let me know what the error message is please?


     


    The CLR Bridge is something very cool, it basically allows you to easily call functions from .NET Assemblies (as documented here) and the dotNET object allows you to access .NET Framework assemblies, types and type members directly, such as System.DateTime, greatly extending Test Complete functionality.





    Regards,


    Phil Baird




  • leandropoblet's avatar
    leandropoblet
    Frequent Contributor
    Hi Phil,



    here is the code that works for me:



    function DateToUTC(dateToBeConverted)

    {

      //  Converts any given datetime to utc

     

      var dt = dotNET["System"]["DateTime"]["Now"];

         

      dt = dt["Parse"]( dateToBeConverted );

      utc = aqConvert["StrToDateTime"]( dt["ToUniversalTime"]() );

         

      //Log["Message"]( utc );

         

      return( utc );

    }



    I don't really use try/catch since it 'hides" errors specially while developing.



    Cheers,

    Leandro



  • leandropoblet's avatar
    leandropoblet
    Frequent Contributor
    Hi Robert,



    thanks for your reply, but that's not what I'm looking for.

    I need to convert any given datetime to UTC since databases usually store UTC.



    Cheers,

    Leandro
  • leandropoblet's avatar
    leandropoblet
    Frequent Contributor
    Hi Phil,



    It works for the actual time, but I can't make it work with this:



    I have to 'convert' from this format: 2014-04-30 08:00:00 as a parameter

    to UTC.



    When I modify your function it fails in the "Parse" step.



    BTW I have never used CLR Bridge and I don't quite understand how it works.