找回密码
 加入
搜索
查看: 1911|回复: 5

[AU3基础] 14位日期时间格式化两种方法

[复制链接]
发表于 2018-10-4 23:10:47 | 显示全部楼层 |阅读模式
本帖最后由 Bone 于 2018-10-4 23:18 编辑

发现查询出来的时间值在数据库查询中正常
从AU3查出来就是'20181004225519'这样的纯数字的
看了看帮助_DateTimeFormat无法直接对这种字符串进行日期时间的格式化

论坛中搜索看到了方法一
补充了一个方法二
仅做备份

$Value = '20181004225519'
;~ 方法一
If StringLen($Value) = 14 Then
        $Data = StringMid($Value, 1, 4) & "-" & StringMid($Value, 5, 2) & "-" & StringMid($Value, 7, 2)
        $Time = StringMid($Value, 9, 2) & ":" & StringMid($Value, 11, 2) & ":" & StringMid($Value, 13, 2)
        $D_T = $Data & " " & $Time
        MsgBox(0, 0, $D_T)
Else
        MsgBox(0, 0, '字符串不合法')
EndIf

;~ 方法二
If StringLen($Value) = 14 Then
        ConsoleWrite(StringFormat("%04i/%02i/%02i %02i:%02i:%02i",StringMid($Value,1,4),StringMid($Value,5,2),StringMid($Value,7,2),StringMid($Value,9,2),StringMid($Value,11,2),StringMid($Value,13,2)) & @CRLF)
EndIf






发表于 2018-10-5 09:44:47 | 显示全部楼层

$str = '20181004225519'
$str = StringRegExpReplace($str, '^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$', '${1}/${2}/${3} ${4}:${5}:${6}')
If @extended <> 0 Then
        MsgBox(0, 0, $str)
Else
        MsgBox(16, 0, '非14位数')
EndIf


评分

参与人数 1金钱 +10 收起 理由
顽固不化 + 10 赞一个!

查看全部评分

发表于 2018-10-5 08:32:56 | 显示全部楼层
方法3:
#include <String.au3>
$Value = '20181004225519'
$new=_StringInsert(_StringInsert(_StringInsert(_StringInsert(_StringInsert($Value,'-',4),'-',7),' ',10),':',13),':',16)
MsgBox(0,0,$new)

 楼主| 发表于 2018-10-5 16:54:34 | 显示全部楼层
本帖最后由 Bone 于 2018-10-5 16:56 编辑

2楼3楼的方法也很棒
发表于 2018-10-5 17:59:35 | 显示全部楼层
要不要再来个 StringSplit  的
 楼主| 发表于 2018-10-5 18:27:04 | 显示全部楼层
绿色风 发表于 2018-10-5 17:59
要不要再来个 StringSplit  的

大神来一个~
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-4-26 09:14 , Processed in 0.086178 second(s), 21 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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