leandropoblet
11 years agoFrequent Contributor
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
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