[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

VBS如何把秒转换成时分秒形式

我知道用以下方法可以把时分秒形式转换成秒:
  1. hms = "23:15:59"
  2. a = split(hms,":")
  3. intHour = a(0)
  4. intMinutes = a(1)
  5. intSeconds = a(2)
  6. intTime = (a(0) * 60 * 60) + (a(1) * 60) + a(2)
  7. Wsh.Echo hms & " = " & intTime & "秒"
复制代码

请问如何把已知的秒数转换成时分秒形式呢?如果秒数够大,转换成“日时分秒”形式又如何?请指点。

vbs中有一大堆日期函数,用不着split去分割

Date 函数 | Day 函数 | Hour 函数 | Minute 函数 | Month 函数 | Second 函数 | Time 函数 | Weekday 函数 | Year 函数


以上函数会将一个标准的now返回时间数据按年月日时分秒提取出来

TOP

假如时间是从一个文本中提取出来的又如何?而且我不是想取得即时时间。

经过试验,用以下方法实现把秒数转换成时分秒形式:
  1. se = 83759
  2. h = Int(se / 3600)
  3. m = Int((se Mod 3600) / 60)
  4. s = (se Mod 3600) Mod 60
  5. if m=0 then m="00"
  6. if m=1 then m="01"
  7. if m=2 then m="02"
  8. if m=3 then m="03"
  9. if m=4 then m="04"
  10. if m=5 then m="05"
  11. if m=6 then m="06"
  12. if m=7 then m="07"
  13. if m=8 then m="08"
  14. if m=9 then m="09"
  15. if s=0 then s="00"
  16. if s=1 then s="01"
  17. if s=2 then s="02"
  18. if s=3 then s="03"
  19. if s=4 then s="04"
  20. if s=5 then s="05"
  21. if s=6 then s="06"
  22. if s=7 then s="07"
  23. if s=8 then s="08"
  24. if s=9 then s="09"
  25. Wsh.Echo h & ":" & m & ":" & s
复制代码

但以上方法不知是否正规。或者有没有更好的方法呢?

[ 本帖最后由 newxso 于 2008-11-13 08:54 编辑 ]

TOP

是正规的吧。

其实这个问题类似于取一个十进制数的百位数、十位数和个位数,是“六十进制”。

TOP

se = 83759
h = se \ 3600
tse = se Mod 3600
m = Right("00" & (tse \ 60),2)
s = Right("00" & (tse Mod 60),2)
WSH.Echo h & ":" & m & ":" & s

TOP

slore兄巧秒地用截取右边2个字符方式来简化 if ... then ... 使整个句子简化了。其实VBS中是否存在保留两位有效整数的函数呢?就好似 FormatNumber(12.3454,2) 可以保留两位有效小数一样。

TOP

返回列表