|
|
TP系列和CTG系列屏时间转换成字符串显示
第一步,提取HMI的系统时间,并转换成字符串
方法一:
var now;
now=new Date();
var year,month,day,hours,min,sec;
// toFixed(n) 格式话字符串,指定四舍五入保留多少小数
year = now.getFullYear().toFixed( 0 );//获取年
month = ( now.getMonth()+1 ).toFixed( 0 );
day = now.getDate().toFixed( 0 );
hours = now.getHours().toFixed( 0 );
min = now.getMinutes().toFixed( 0 );
sec = now.getSeconds().toFixed( 0 );
// 不够两位补0
month = ( month.length == 1 ) ? ( "0" + month ) : month;
day = ( day.length == 1 ) ? ( "0" + day ) : day;
hours = ( hours.length == 1 ) ? ( "0" + hours ) : hours;
min = ( min.length == 1 ) ? ( "0" + min ) : min;
sec = ( sec.length == 1 ) ? ( "0" + sec ) : sec;
SetValue( smartTags('变量_1'), year+"-"+month+"-"+day+" "+hours+":"+min+":"+sec );
方法二:
var now;
now=new Date();
var year,month,day,hours,min,sec;
// .toString()转成字符串.split( "." )以"."切割成字符串数组
year =( now.getFullYear() +0.1 ).toString().split( "." )[0];//获取年
month =( now.getMonth() +1.1 ).toString().split( "." )[0];
day =( now.getDate() +0.1 ).toString().split( "." )[0];
hours =( now.getHours() +0.1 ).toString().split( "." )[0];
min =( now.getMinutes() +0.1 ).toString().split( "." )[0];
sec =( now.getSeconds() +0.1 ).toString().split( "." )[0];
// 不够两位补0
month = ( month.length == 1 ) ? ( "0" + month ) : month;
day = ( day.length == 1 ) ? ( "0" + day ) : day;
hours = ( hours.length == 1 ) ? ( "0" + hours ) : hours;
min = ( min.length == 1 ) ? ( "0" + min ) : min;
sec = ( sec.length == 1 ) ? ( "0" + sec ) : sec;
SetValue( smartTags('变量_2'), year+"-"+month+"-"+day+" "+hours+":"+min+":"+sec );
第二步,调用脚本
第三步,用字符IO与显示出来
|
|