{"id":196,"date":"2010-01-07T12:42:58","date_gmt":"2010-01-07T01:42:58","guid":{"rendered":"http:\/\/threemillion.net\/blog\/?p=196"},"modified":"2010-03-14T20:43:42","modified_gmt":"2010-03-14T09:43:42","slug":"generate-iso-date-time-in-a-batch-file","status":"publish","type":"post","link":"https:\/\/threemillion.net\/blog\/?p=196","title":{"rendered":"Generate ISO Date Time in a Batch File"},"content":{"rendered":"<p>I was looking for a way to generate a log file name variable containing the current date and time. Yes normally I use vbscript and moving to Powershell however it is fun sometimes to rediscover old skills.<\/p>\n<p>Now [cmd]date \/t[\/cmd] produces (at the cmd.exe in Windows XP) the format [cmd]Day DD\/MM\/YYYY[\/cmd] for example: [cmd]Thu 07\/01\/2010[\/cmd]<\/p>\n<p>Attempting to assign this to a variable like so, does not work<br \/>\n[cmd]SET datenow=date \/t[\/cmd]<br \/>\nWhen echoed out this time returns the string: [cmd]date \/t[\/cmd]<\/p>\n<p>[cmd]SET datenow=%date%[\/cmd]<br \/>\nThis will now set the variable to the current value of the date. There is no need for the \/T.<\/p>\n<p>The same process can be used with setting a variable with the current time: [cmd]SET timenow=%time%[\/cmd] which returns the format [cmd]HH:MM:SS.mm[\/cmd] Where mm is hundredths of a second.<br \/>\nExample output:[cmd]12:09:58.90[\/cmd]<\/p>\n<p>So what I was after was something in the YYYY-MM-DD-HH-MM-SS format. Now that I know how to get the current date and time how do I get the components of them to satisfy the format?<\/p>\n<p>It is possible to return a substring of a variable. I must confess here that I do not the full ins and outs of how the substring capability works howerver I know enough to be dangerous. \ud83d\ude42 Basically the format is [cmd]%variable:~num_chars_to_skip,num_chars_to_keep%[\/cmd] <\/p>\n<p>So how do I return the year only?<br \/>\n[cmd]SET yearnow=%datenow:~10,4%[\/cmd]<br \/>\nReturns the year by starting at character 10 and returning the next 4 characters, this example 2010.<\/p>\n<p>Or you can even use the date command, straight up.<br \/>\n[cmd]SET yearnow=%date:~10,4%[\/cmd]<\/p>\n<p>Now I simply just need to get all the substrings together into one variable. I could do this a number of ways, one being create a whole bunch of variables for each component, which is long but easy to follow or I can make it a one liner, which is what I chose to do. Here is the result:<br \/>\n[cmd]<br \/>\necho Get the current date and time in YYYY-MM-DD-HH-MM-SS format<br \/>\nSET isodate=%date:~10,4%-%date:~7,2%-%date:~4,2%-%time:~0,2%-%time:~3,2%-%time:~6,2%<br \/>\necho %isodate%<br \/>\n[\/cmd]<\/p>\n<p>This returns: [cmd]2010-01-07-12-09-58[\/cmd]<\/p>\n<p>Basically the isodate variable is created by multiple instances of the date and time and combining the use of substring manipulation. Now I can use this variable as part of my log file naming.<\/p>\n<p><strong>References:<\/strong><br \/>\n<a href=\"http:\/\/ss64.com\/ntsyntax\/varsubstring.html\">http:\/\/ss64.com\/ntsyntax\/varsubstring.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was looking for a way to generate a log file name variable containing the current date and time. Yes normally I use vbscript and moving to Powershell however it <a href=\"https:\/\/threemillion.net\/blog\/?p=196\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[10],"tags":[29],"class_list":["entry","author-admin","post-196","post","type-post","status-publish","format-standard","category-batch","tag-iso-date"],"_links":{"self":[{"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/196","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=196"}],"version-history":[{"count":6,"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/196\/revisions"}],"predecessor-version":[{"id":261,"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/196\/revisions\/261"}],"wp:attachment":[{"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/threemillion.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}