如何在cmd中检索TIME的秒部分? [英] How can I retrieve the seconds part of TIME in cmd?

查看:62
本文介绍了如何在cmd中检索TIME的秒部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个cmd脚本,以获取当前日期和时间,并将其格式化为sqlserver可以将其输入为 datetime 的方式.

I'm trying to write a cmd script that gets the current date and time and formats it into a way that sqlserver can input it as a datetime.

到目前为止,我有:

@echo off
for /F "tokens=1-4 delims=/ " %%i in ('date /t') do (
set dow=%%i
set mon=%%j
set day=%%k
set yr=%%l
set mydate=%%j/%%k/%%l
)

此打印输出2010年10月22日

This prints out 10/22/2010

我还无法弄清楚如何使时间变成可用的格式.我尝试使用 time/t ,但是它只给出小时和分钟,而我也需要秒.

I have not been able to figure out how to get the time into a usable format. I tried working with time /t, but it only gives the hours and minutes, and I need the seconds also.

推荐答案

使用%TIME%伪变量.

您也可以在上述脚本中使用%DATE%.

You can also use %DATE% in above script.

两者都有相同的限制,因为它们取决于您的区域设置.该代码不是可移植的,在其他环境中,您会感到惊讶.但这就是cmd中的日期和时间的工作方式.

Both have the same limitations, in that they depend on your locale. The code is not portable and you'll be up for surprises in other environments. But that's how date and time in cmd works.

这篇关于如何在cmd中检索TIME的秒部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆