tags:

views:

99

answers:

2

I can use following to append a date to a text:

"Foo {0:G} Foo" -f (date)     #returns "Foo 2009-12-07 15:34:16 Foo"

But I want the time in Unix format. I can get it by date -UFormat %s, but can I use the same syntax?

When I use -UFormat %s I get 1260199855,65625, how do I remove the decimal?

+1  A: 

Just cast the result to an int like so:

PS> [int][double]::Parse((Get-Date -UFormat %s))
1260172909

PS> "Foo {0:G} Foo" -f [int][double]::Parse((Get-Date -UFormat %s))
Foo 1260172997 Foo

Using the Parse method means the string is parsed "culture aware" such that the appropriate decimal separator character is recognized for the current culture. If you just cast directly, PowerShell uses the invariant culture which causes problems for any culture where decimal sep char is not a period.

Keith Hill
If I use [int](get-date -UFormat %s), I get Cannot convert value "123456789,12345" to type "System.Int32". Error: "Input string was not in a correct format."
magol
Hmm, wonder if this is a localization bug?
Keith Hill
Apparently, PowerShell uses invariant culture when coercing from a string to a number which means only a period is recognized as the decimal separator character. I've updated the answer to accomodate.
Keith Hill
A: 

My solution:

(Get-Date -UFormat %s) -Replace("[,\.]\d*", "")
magol