Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Date Problem

Posted on 2013-06-07
8
Medium Priority
?
259 Views
Last Modified: 2013-06-11
I have a batch file in it I have the following:

set "mm=%date:~4,2%   This gives me 06, just what I want
set /a monthback=%date:~4,2%-1 this gives me 5

How can I make set /a monthback=%date:~4,2%-1 gives me 05, instead of 5?

Thank you,
Lori
0
Comment
Question by:NLITech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 20

Accepted Solution

by:
dsacker earned 2000 total points
ID: 39230755
set /a monthback=%date:~4,2%+99
set monthback=%monthback:~-2%
0
 

Author Closing Comment

by:NLITech
ID: 39230771
Thank you!
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39230795
Be careful there.... 0x is octal so it WILL break in August if your date format has leading zeroes.

set /a month=09+99
Invalid number.  Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).

If you know that it will have leading zeroes try:

et /a monthback=1%date:~4,2%+99
set monthback=%monthback:~-2%

i.e 108+99 = 207

Much better use something like this independent of your date format:
http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html

Steve
0
How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

 
LVL 58

Expert Comment

by:Bill Prew
ID: 39230939
I have some thoughts on this, but would like to understand what you need.  It looks like you want to take the current month, and get the prior month?  If so, how do you want the result formatted, with a leading zero to always be two characters, or just a 1 or 2 digit number without any leading 0.  What do you want to happen in January, do you want to get 0 or 12 as the "prior month"?

~bp
0
 
LVL 58

Expert Comment

by:Bill Prew
ID: 39230979
Here's how I'd go about it.

@echo off
REM Add 100 to current month, subtract 1
set /a MonthBack=1%DATE:~4,2%-1
REM If current month was February, "wrap" to December (and not 00)
if %MonthBack% == 100 set MonthBack=112
REM Get right two digits of result so that we are zero padded on the left
set MonthBack=%MonthBack:~-2%
REM Display prior month
echo %MonthBack%

Open in new window

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39231204
Bill - agreed, was just pointing out what he had was going to break being as this was already accepted.
0
 

Author Comment

by:NLITech
ID: 39238527
Bp is right, this did break %mm%. I did try Bp solution, and now %mm% works, but %monthback% is only giving me 5.
I will post a new better worded (hopefully) post.  

Thank you all,
Lori
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39238581
If your date is in leading zero format Bill's script should give you the right month OK with the leadfing 0.  If you don't have leading zero then your original one should be OK.

Personally though I would do as I suggested and use VBScript or util to do it.

You could also just schedule your script to run on the last day of the previous month :-)

Steve
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question