合信论坛

快捷导航
查看: 483|回复: 0

TP系列和CTG系列屏时间转换成字符串显示

[复制链接]

92

主题

98

帖子

1107

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1107
发表于 2026-3-11 14:27:20 | 显示全部楼层 |阅读模式
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 );


第二步,调用脚本

调用脚本.png

第三步,用字符IO与显示出来

显示效果.png

Script_DateTime.zip

110.49 KB, 下载次数: 0

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

客服热线
400-700-4858 周一至周五:09:00 - 18:00
深圳市南山区打石一路深圳国际创新谷6栋A座9层

深圳市合信自动化技术有限公司(简称“合信技术”)成立于2003年,高新技术企业,专注于工业自动化产品的研发、生产、销售和技术服务,依靠高质量、高性能的自动化控制产品与方案为客户创造最大价值,立志于成为全球领先的工业自动化解决方案供应商。

Archiver|手机版|小黑屋|COTRUST Inc. ( 粤ICP备13051915号 )

GMT+8, 2026-6-14 23:15 , Processed in 0.021547 second(s), 21 queries .

快速回复 返回顶部 返回列表