Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - date formatting

Reply
 
Old 03-31-2008   #1 (permalink)
Rob Pettrey


 
 

date formatting

I'm building a PowerShell script for ntbackup, starting with the date:
$today = get-date
Monday, March 31, 2008 7:48:58 PM

then the day of the week:
$day_of_week = $today.dayofweek
Monday

then the output file name:
$name_tag = get-date -uformat "%a"
$backup_bkf = get-date -uformat "%Y-%m-%d-$name_tag.bkf"
2008-03-31-Mon.bkf

But I really wanted to use the same time/date stamp value throughout.

Instead of this:
$backup_bkf = get-date -uformat "%Y-%m-%d-$name_tag.bkf"

How can I do this:
$backup_bkf = $today -uformat "%Y-%m-%d-$name_tag.bkf"

--
Rob Pettrey
Microsoft Small Business Specialist

My System SpecsSystem Spec
Old 03-31-2008   #2 (permalink)
Kiron


 
 

Re: date formatting

$today = get-date

# multiple assignment
$day_of_Week, $name_tag = "$('{0:dddd},{0:ddd}' -f $today)".split(',')
$day_of_Week
$name_tag

# string concatenation
$backup_bkf = $($today.toString('yyyy-MM-dd')) + "-$name_tag.bkf"
$backup_bkf

# subexpression string expansion
$backup_bkf = "$($($today.toString('yyyy-MM-dd')))-$name_tag.bkf"
$backup_bkf

# safer subexpression string expansion - note the curly braces
$backup_bkf = "$($($today.toString('yyyy-MM-dd')))-${name_tag}.bkf"
$backup_bkf

# # # #

# other samples w/o $name_tag
$today = get-date
$backup_bkf = '{0:yyyy-MM-dd-ddd}.bkf' -f $today
$backup_bkf

$today = get-date
$backup_bkf = "$($today.toString('yyyy-MM-dd-ddd')).bkf"
$backup_bkf

--
Kiron
My System SpecsSystem Spec
Old 04-01-2008   #3 (permalink)
Kiron


 
 

Re: date formatting

Excuse the typo, no subexpression needed:

# string concatenation
$backup_bkf = $today.toString('yyyy-MM-dd') + "-$name_tag.bkf"
$backup_bkf

--
Kiron
My System SpecsSystem Spec
Old 04-01-2008   #4 (permalink)
Kiron


 
 

Re: date formatting

$today = get-date

# multiple assignment
$day_of_Week, $name_tag = "$('{0:dddd},{0:ddd}' -f $today)".split(',')
$day_of_Week
$name_tag

# string concatenation
$backup_bkf = $today.toString('yyyy-MM-dd') + "-$name_tag.bkf"
$backup_bkf

# subexpression string expansion
$backup_bkf = "$($today.toString('yyyy-MM-dd'))-$name_tag.bkf"
$backup_bkf

# safer subexpression string expansion - note the curly braces
$backup_bkf = "$($today.toString('yyyy-MM-dd'))-${name_tag}.bkf"
$backup_bkf

# other samples
$today = get-date
$backup_bkf = '{0:yyyy-MM-dd-ddd}.bkf' -f $today
$backup_bkf

$today = get-date
$backup_bkf = "$($today.toString('yyyy-MM-dd-ddd')).bkf"
$backup_bkf


--
Kiron
My System SpecsSystem Spec
Old 04-07-2008   #5 (permalink)
Rob Pettrey


 
 

Re: date formatting

Kiron,

Perfect! Just what I needed!

I ended up with:

$backup_bkf = $today.toString('yyyy-MM-dd') + "-$name_tag.bkf"

since it seemed the most intuitive to me.

thanks again for the many examples! Very helpful!

--
Rob Pettrey
Microsoft Small Business Specialist


"Kiron" wrote:
Quote:

> $today = get-date
>
> # multiple assignment
> $day_of_Week, $name_tag = "$('{0:dddd},{0:ddd}' -f $today)".split(',')
> $day_of_Week
> $name_tag
>
> # string concatenation
> $backup_bkf = $today.toString('yyyy-MM-dd') + "-$name_tag.bkf"
> $backup_bkf
>
> # subexpression string expansion
> $backup_bkf = "$($today.toString('yyyy-MM-dd'))-$name_tag.bkf"
> $backup_bkf
>
> # safer subexpression string expansion - note the curly braces
> $backup_bkf = "$($today.toString('yyyy-MM-dd'))-${name_tag}.bkf"
> $backup_bkf
>
> # other samples
> $today = get-date
> $backup_bkf = '{0:yyyy-MM-dd-ddd}.bkf' -f $today
> $backup_bkf
>
> $today = get-date
> $backup_bkf = "$($today.toString('yyyy-MM-dd-ddd')).bkf"
> $backup_bkf
>
>
> --
> Kiron
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
date time formatting .NET General
Modified Date used as Date Taken in Photo Gallery and Digital Imag Vista music pictures video
Photo date taken vs. file date Vista music pictures video
Problem formatting date PowerShell
Blank "Date Taken / Date Modified" field in Vista Vista file management


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46