![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Concatenate variables with numbers Hello everyone, I'm having some issues getting a variable valued the way I want it. I'm using the get-date cmdlet to get the current date from the system, I'm only concerned with the year and month, but I want to set the variable to last month. In the case of March 2008, I can easily set my variable to February 2008 like this: (get-date -uformat "%Y%m") -1 The problem is that if it were to be January 2008, using the above, my variable would be valued to this: 200800 Shouldn't it know that this is a date since I directly used the get- date cmdlet? So, I've tried making the value returned from get-date a string like this: ((get-date).toString('yyyy'))-1 The only problem is that when I go to concatenate it with a month value, it always adds the two numeric values so instead of getting a variable valued like this: 200712 I get one valued like this: 2019 I have searched and can not find a way to concatenate the value of the strings without adding them. I've tried the following: $YM = $MoveYear + $MoveMonth $YM = $MoveYear += $MoveMonth Both give the same result. Thank you. |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Concatenate variables with numbers This may be simpler ... (get-date "1/1/2008").AddMonths(-1).Month (get-date "1/1/2008").AddMonths(-1).Year -- Jon "Orrin" <oedenfield@xxxxxx> wrote in message news:96e8203d-4371-4428-87bb-ae573875f9a2@xxxxxx Quote: > Hello everyone, > > I'm having some issues getting a variable valued the way I want it. > I'm using the get-date cmdlet to get the current date from the system, > I'm only concerned with the year and month, but I want to set the > variable to last month. > > In the case of March 2008, I can easily set my variable to February > 2008 like this: > > (get-date -uformat "%Y%m") -1 > > The problem is that if it were to be January 2008, using the above, my > variable would be valued to this: > > 200800 > > Shouldn't it know that this is a date since I directly used the get- > date cmdlet? > > So, I've tried making the value returned from get-date a string like > this: > > ((get-date).toString('yyyy'))-1 > > The only problem is that when I go to concatenate it with a month > value, it always adds the two numeric values so instead of getting a > variable valued like this: > > 200712 > > I get one valued like this: > > 2019 > > I have searched and can not find a way to concatenate the value of the > strings without adding them. I've tried the following: > > $YM = $MoveYear + $MoveMonth > $YM = $MoveYear += $MoveMonth > > Both give the same result. > > Thank you. |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Concatenate variables with numbers Thank you for your help. Using this method to set the dates of the variables still yield me two variables that when I concatenate them I get 2019. Also will this method work when get-date returns a date like 01/01/2010 ? I should have clarified; I am using this in a script that I want to have setup so that no matter what the year is, if get- date returns a MM of 01, then it will be able to set a variable as yyyyMM (December of the previous year). For some reason when Powershell is returning a DATE is not letting me subtract 1 month and get December of the previous year (it will easily show a month of 00 instead). I just tried: (get-date).AddMonths(-1) -uformat "%Y%m" and (get-date -uformat "%Y%m").AddMonths(-1) and they returned errors. I must be missing something. Orrin On Mar 5, 2:21 pm, "Jon" <Email_Addr...@xxxxxx> wrote: Quote: > This may be simpler ... > > (get-date "1/1/2008").AddMonths(-1).Month > (get-date "1/1/2008").AddMonths(-1).Year > > -- > Jon > > "Orrin" <oedenfi...@xxxxxx> wrote in message > > news:96e8203d-4371-4428-87bb-ae573875f9a2@xxxxxx > Quote: > > Hello everyone, Quote: > > I'm having some issues getting a variable valued the way I want it. > > I'm using the get-date cmdlet to get the current date from the system, > > I'm only concerned with the year and month, but I want to set the > > variable to last month. Quote: > > In the case of March 2008, I can easily set my variable to February > > 2008 like this: Quote: > > (get-date -uformat "%Y%m") -1 Quote: > > The problem is that if it were to be January 2008, using the above, my > > variable would be valued to this: Quote: > > 200800 Quote: > > Shouldn't it know that this is a date since I directly used the get- > > date cmdlet? Quote: > > So, I've tried making the value returned from get-date a string like > > this: Quote: > > ((get-date).toString('yyyy'))-1 Quote: > > The only problem is that when I go to concatenate it with a month > > value, it always adds the two numeric values so instead of getting a > > variable valued like this: Quote: > > 200712 Quote: > > I get one valued like this: Quote: > > 2019 Quote: > > I have searched and can not find a way to concatenate the value of the > > strings without adding them. I've tried the following: Quote: > > $YM = $MoveYear + $MoveMonth > > $YM = $MoveYear += $MoveMonth Quote: > > Both give the same result. Quote: > > Thank you. |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Concatenate variables with numbers Hi Orrin, It doesn't work because the -uformat is a parameter for get-date and for AddMonths() method. Try this instead: PS > (get-date).AddMonths(-1).ToString("yyyyMM") 200802 Another way to get it is with the .NET string format operator: PS > "{0:yyyyMM}" -f (get-date).AddMonths(-1) 200802 ----- Shay Levi $cript Fanatic http://scriptolog.blogspot.com Quote: > Thank you for your help. > > Using this method to set the dates of the variables still yield me two > variables that when I concatenate them I get 2019. > > Also will this method work when get-date returns a date like > 01/01/2010 ? I should have clarified; I am using this in a script > that I want to have setup so that no matter what the year is, if get- > date returns a MM of 01, then it will be able to set a variable as > yyyyMM (December of the previous year). For some reason when > Powershell is returning a DATE is not letting me subtract 1 month and > get December of the previous year (it will easily show a month of 00 > instead). > > I just tried: > > (get-date).AddMonths(-1) -uformat "%Y%m" > and > (get-date -uformat "%Y%m").AddMonths(-1) > and they returned errors. > > I must be missing something. > > Orrin > > On Mar 5, 2:21 pm, "Jon" <Email_Addr...@xxxxxx> wrote: > Quote: >> This may be simpler ... >> >> (get-date "1/1/2008").AddMonths(-1).Month >> (get-date "1/1/2008").AddMonths(-1).Year >> -- >> Jon >> "Orrin" <oedenfi...@xxxxxx> wrote in message >> >> news:96e8203d-4371-4428-87bb-ae573875f9a2@xxxxxx >> .. >> Quote: >>> Hello everyone, >>> >>> I'm having some issues getting a variable valued the way I want it. >>> I'm using the get-date cmdlet to get the current date from the >>> system, I'm only concerned with the year and month, but I want to >>> set the variable to last month. >>> >>> In the case of March 2008, I can easily set my variable to February >>> 2008 like this: >>> >>> (get-date -uformat "%Y%m") -1 >>> >>> The problem is that if it were to be January 2008, using the above, >>> my variable would be valued to this: >>> >>> 200800 >>> >>> Shouldn't it know that this is a date since I directly used the get- >>> date cmdlet? >>> >>> So, I've tried making the value returned from get-date a string like >>> this: >>> >>> ((get-date).toString('yyyy'))-1 >>> >>> The only problem is that when I go to concatenate it with a month >>> value, it always adds the two numeric values so instead of getting a >>> variable valued like this: >>> >>> 200712 >>> >>> I get one valued like this: >>> >>> 2019 >>> >>> I have searched and can not find a way to concatenate the value of >>> the strings without adding them. I've tried the following: >>> >>> $YM = $MoveYear + $MoveMonth >>> $YM = $MoveYear += $MoveMonth >>> Both give the same result. >>> >>> Thank you. >>> |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Concatenate variables with numbers "Orrin" <oedenfield@xxxxxx> wrote in message news:67b12cc6-fc47-47bd-a196-03d9086138ec@xxxxxx Quote: > Thank you for your help. > > Using this method to set the dates of the variables still yield me two > variables that when I concatenate them I get 2019. > Shay's given you the solution, to which I'd only add that the reason you got 2019 was that you were adding 2 integers 2007 and 12. To concatenate, you'd cast to strings eg eg compare these two 2007 + 12 [string]2007 + [string]12 -- Jon |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Concatenate variables with numbers That is it! Thank you so much! I knew there had to be a simple way to to do it. Orrin On Mar 5, 3:36 pm, Shay Levi <n...@xxxxxx> wrote: Quote: > Hi Orrin, > > It doesn't work because the -uformat is a parameter for get-date and for > AddMonths() method. > > Try this instead: > > PS > (get-date).AddMonths(-1).ToString("yyyyMM") > 200802 > > Another way to get it is with the .NET string format operator: > > PS > "{0:yyyyMM}" -f (get-date).AddMonths(-1) > 200802 > > ----- > Shay Levi > $cript Fanatichttp://scriptolog.blogspot.com > Quote: > > Thank you for your help. Quote: > > Using this method to set the dates of the variables still yield me two > > variables that when I concatenate them I get 2019. Quote: > > Also will this method work when get-date returns a date like > > 01/01/2010 ? I should have clarified; I am using this in a script > > that I want to have setup so that no matter what the year is, if get- > > date returns a MM of 01, then it will be able to set a variable as > > yyyyMM (December of the previous year). For some reason when > > Powershell is returning a DATE is not letting me subtract 1 month and > > get December of the previous year (it will easily show a month of 00 > > instead). Quote: > > I just tried: Quote: > > (get-date).AddMonths(-1) -uformat "%Y%m" > > and > > (get-date -uformat "%Y%m").AddMonths(-1) > > and they returned errors. Quote: > > I must be missing something. Quote: > > Orrin Quote: > > On Mar 5, 2:21 pm, "Jon" <Email_Addr...@xxxxxx> wrote: Quote: Quote: > >> This may be simpler ... Quote: Quote: > >> (get-date "1/1/2008").AddMonths(-1).Month > >> (get-date "1/1/2008").AddMonths(-1).Year > >> -- > >> Jon > >> "Orrin" <oedenfi...@xxxxxx> wrote in message Quote: Quote: > >>news:96e8203d-4371-4428-87bb-ae573875f9a2@xxxxxx > >> .. Quote: Quote: > >>> Hello everyone, Quote: Quote: > >>> I'm having some issues getting a variable valued the way I want it. > >>> I'm using the get-date cmdlet to get the current date from the > >>> system, I'm only concerned with the year and month, but I want to > >>> set the variable to last month. Quote: Quote: > >>> In the case of March 2008, I can easily set my variable to February > >>> 2008 like this: Quote: Quote: > >>> (get-date -uformat "%Y%m") -1 Quote: Quote: > >>> The problem is that if it were to be January 2008, using the above, > >>> my variable would be valued to this: Quote: Quote: > >>> 200800 Quote: Quote: > >>> Shouldn't it know that this is a date since I directly used the get- > >>> date cmdlet? Quote: Quote: > >>> So, I've tried making the value returned from get-date a string like > >>> this: Quote: Quote: > >>> ((get-date).toString('yyyy'))-1 Quote: Quote: > >>> The only problem is that when I go to concatenate it with a month > >>> value, it always adds the two numeric values so instead of getting a > >>> variable valued like this: Quote: Quote: > >>> 200712 Quote: Quote: > >>> I get one valued like this: Quote: Quote: > >>> 2019 Quote: Quote: > >>> I have searched and can not find a way to concatenate the value of > >>> the strings without adding them. I've tried the following: Quote: Quote: > >>> $YM = $MoveYear + $MoveMonth > >>> $YM = $MoveYear += $MoveMonth > >>> Both give the same result. Quote: Quote: > >>> Thank you. |
My System Specs![]() |
| | #7 (permalink) |
| | Re: Concatenate variables with numbers Thank you for your help Jon. I really appreciate it. Orrin On Mar 5, 3:59 pm, "Jon" <Email_Addr...@xxxxxx> wrote: Quote: > Shay's given you the solution, to which I'd only add that the reason you got > 2019 was that you were adding 2 integers 2007 and 12. To concatenate, you'd > cast to strings eg > > eg compare these two > > 2007 + 12 > [string]2007 + [string]12 > > -- > Jon |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| math with "GB/MB/KB" in variables fails, without variables works? | PowerShell | |||
| How to concatenate a literal string and a variable value? | VB Script | |||
| How to Concatenate text files? | PowerShell | |||