欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Dos下获取系统时间的代码

程序员文章站 2022-06-23 18:53:29
复制代码 代码如下:  rem dos get current time  rem =======================  rem...

复制代码 代码如下:
 
rem dos get current time 
rem ======================= 
rem get yyyy/mm/dd 
set mydate=%date:~0,10% 
echo %mydate% 
rem ======================= 
rem get yyyy 
set yyyy=%date:~0,4% 
echo %yyyy% 
rem ======================= 
rem get mm 
set mm=%date:~5,2% 
echo %mm% 
rem ======================= 
rem get dd 
set dd=%date:~8,2% 
echo %dd% 
rem ======================= 
rem get yyyy-mm-dd 
set curdate=%yyyy%-%mm%-%dd% 
echo %curdate%


其实bat中获取时间就是利用截取日期字符串的方法

dos下有字符处理的方法的,总结如下:

在这个日期格式下:”2013-07-22 星期一”, 这个%date:~0,10%得到的是2013-07-22
说明:date为系统变量,“:”表示开始处理前面的字符串“~0,10”表示从第0个字符开始取10个字符。

输出:

c:\documents and settings\administrator>rem dos get current time
c:\documents and settings\administrator>rem =======================
c:\documents and settings\administrator>rem get yyyy/mm/dd
c:\documents and settings\administrator>set mydate=2013-07-22
c:\documents and settings\administrator>echo 2013-07-22
2013-07-22
c:\documents and settings\administrator>rem =======================
c:\documents and settings\administrator>rem get yyyy
c:\documents and settings\administrator>set yyyy=2013
c:\documents and settings\administrator>echo 2013
2013
c:\documents and settings\administrator>rem =======================
c:\documents and settings\administrator>rem get mm
c:\documents and settings\administrator>set mm=07
c:\documents and settings\administrator>echo 07
07
c:\documents and settings\administrator>rem =======================
c:\documents and settings\administrator>rem get dd
c:\documents and settings\administrator>set dd=22
c:\documents and settings\administrator>echo 22
22
c:\documents and settings\administrator>rem =======================
c:\documents and settings\administrator>rem get yyyy-mm-dd
c:\documents and settings\administrator>set curdate=2013  -07  -22
c:\documents and settings\administrator>echo 2013  -07  -22
2013  -07  -22
c:\documents and settings\administrator>