批量获取当前日期月份和年份的安全方法 [英] Safe way to get current day month and year in batch

查看:65
本文介绍了批量获取当前日期月份和年份的安全方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个批处理脚本,该脚本将以安全的方式(即与日期格式无关)获取当前日期,月份和年份.不幸的是,我发现的所有互联网资源都提出了类似的建议:

I am trying to write a batch script that will get the current day, month and year in a safe manner(i.e. independent of the date format). Unfortunately all the internet sources I have found propose something like:

   SET CURRENT_DATE=%DATE: =0%
   SET YEAR=%DATE:~-4%
   SET MONTH=%DATE:~3,2%
   SET DAY=%DATE:~5,2%

如果我的日期格式为yyyy/mm/dd或yyyy-mm-dd,则此功能有效,但如果日期格式为yy-mm-dd或其他格式,则无效.有没有安全的方法来获取年,月和日?

This will work if my date is in format yyyy/mm/dd or yyyy-mm-dd but will not if it is in format yy-mm-dd or some other format. Is there a safe way to get the year, month and day?

还有一个问题-是否有可能知道日期分隔符(第一种情况为/,其他情况为-).

And one more question - is it possible to know what the day separator is(/ in the first case and - in the other cases).

推荐答案

首先,您可以从注册表中(某个地方)检索当前设置.
格式以及分隔符.

First, you could retrieve the current settings from the registry (somewhere).
The format and also the separators.

但是使用wmic更容易,因为它始终具有相同的格式,唯一的缺点是每次启动时都需要花费一些时间.

But it's easier to use wmic instead, it has always the same format, the only drawback is, that it's take a bit of time, each time you start it.

来自 SO:日志日期/时间

@echo off
setlocal EnableDelayedExpansion
FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
    if "%%B" NEQ "" (
        SET /A FDATE=%%F*10000+%%D*100+%%A
        SET /A FTIME=%%B*10000+%%C*100+%%E
    )
)
ECHO !FDATE! - !FTIME!

这篇关于批量获取当前日期月份和年份的安全方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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