English 中文(简体)
How to get a UNIVERSAL Windows batch file timestamp
原标题:

I m having trouble generating a timestamp in a Windows batch file, because I get diferent date formats on different Windows versions.

My machine:

>echo %date%
>Tue 11/17/2009

Friends machine:

>echo %date%
>11/17/2009

I guess there has to be some way of getting the date (11/17/2009) from both strings using for /f. I ve been trying and googling and can t find the answer.

Is there another way to get a timestamp without using %date%?

最佳回答

Use VBScript if you want to get independent date time settings:

thedate = Now
yr = Year(thedate)
mth = Month(thedate)
dy = Day(thedate)
hr = Hour(thedate)
min = Minute(thedate)
sec = Second(thedate)
WScript.Echo yr&mth&dy&hr&min&sec
问题回答

Check out doff.exe. I use this a lot for getting timestamps for naming log files. From its web site:

DOFF prints a formatted date and time, with an optional date offset, (e.g -1 prints yesterday s date, +1 prints tomorrow s date). To view all the options available, execute "doff -h". I typically use this utility for renaming log files so that they include a timestamp, (see the third example below). This code should compile under Unix/Linux, as well as DOS.

Sample commands:

C:>doff
19991108131135

With no parameters the output is the current date/time in the following format: yyyymmddhhmiss

C:>doff mm/dd/yyyy
11/08/1999

In the above example a date format specification is given.

@echo off
for /f "tokens=1-3 delims=/ " %%a in ( doff mm/dd/yyyy -1 ) do (
set mm=%%a
set dd=%%b
set yyyy=%%c)
rename httpd-access.log httpd-access-%yyyy%%mm%%dd%.log

The sample batch file above shows a neat way to rename a log file based on yesterday s date. The "for" command executes doff to print yesterday s date, (the "-1" parameter specifies yesterday), then extracts each component of the date into DOS batch file variables. The "rename" command renames "httpd-access.log" to "httpd-access-[yesterday s date].log"


Also check out Microsoft s now.exe, available in the Windows Server 2003 Resource Kit Tools. One bad thing I found out (the hard way) about it is it sets the ERRORLEVEL to the number of characters printed.

Looks like this:

c:>now

Thu May 19 14:26:45 2011

Help:

NOW   :  Display Message with Current Date and Time

Usage : NOW [message to be printed with time-stamp]

    NOW displays the current time, followed by its command-line arguments.
    NOW is similar to the standard ECHO command, but with a time-stamp.

Unfortunately, it can t be done directly, so you need to resort to hacks like GetDate.cmd.

There are lots of VBScript and small external commandline tools available too, which isn t something I d take a dependency on unless you re already using something of that nature in your overall system.

Personally, I d be trying to route around it by using PowerShell which neatly sidesteps the issue completely.

You don t need VBScript. You can do it with something like this:

echo %date:~-10,2%/%date:~-7,2%/%date:~-4,4%

Source

As I have posted in here: Batch: Timestamp to UNIX Time

What about simple 1-line long C program returning UNIX timestamp? You can retrieve value from %errorlevel% in batch script.

#include <stdio.h>
#include <time.h>

int main(void)
{
    return (int) time(NULL);
}

In my test in command prompt it worked:

C:UsersdabaranDesktopcs50srcC>.	ime || echo %errorlevel% && set mytstamp=%errorlevel%
1419609373

C:UsersdabaranDesktopcs50srcC>echo %mytstamp%
1419609373




相关问题
Change the date format in kibana

I am working for my internship on the implementation of the ElasticSearch family (ElasticSearch+ Kibana+ logstash). Here is my question: I have a field "@ timestamp" with the following format: 2014-05-...

Quickly getting to YYYY-mm-dd HH:MM:SS in Perl

When writing Perl scripts I frequently find the need to obtain the current time represented as a string formatted as YYYY-mm-dd HH:MM:SS (say 2009-11-29 14:28:29). In doing this I find myself taking ...

Sorting by unknown column type in mysql

So I ve got a users table; storing things like username, email, etc. I then have a user_settings table. Instead of standard columns like timestamp_logged_in, profile_views, etc., I have it organized ...

timestamp date today

Hi guys could you please help me with this. I know it is silly but can t find unswer anywhere: I have a query that shows events from the today time_stamp but I want to include today as well. How do I ...

connection time zone issue with jOra eclipse plugin

I started using the jOra eclipse plugin. The plugin seems pretty robust and I m hoping to stop using SQLDeveloper for 95% of my database needs. Many of our tables have columns of type TIMESTAMP ...

How to get a UNIVERSAL Windows batch file timestamp

I m having trouble generating a timestamp in a Windows batch file, because I get diferent date formats on different Windows versions. My machine: >echo %date% >Tue 11/17/2009 Friends machine: ...

热门标签