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...
  • Philip_Baird's avatar
    11 years ago

    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

  • leandropoblet's avatar
    11 years ago
    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