Reza Amya
Reza Amya

Reputation: 1724

How get azure storage table timestamp as string?

I can get storage table rows using this PowerShell command:

$temp_table_name = "MY_TABLE_NAME"
$saContext = (Get-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccount).Context
$temp_table = Get-AzureStorageTable -Name $temp_table_name -Context $saContext
$result = Get-AzureStorageTableRowAll -table $temp_table

I have two Timestamp in my table, one is for azure table itself with name Timestamp and another one is for Metrics (stored automatically) with name TIMESTAMP. then the result of $result[0] is like bellow:

Average      : 3228132966.4
Count        : 240
CounterName  : \Memory\AvailableMemory
DeploymentId : 2c90752b-100f-4640-a056-e2b894bf5bd5
Host         : cpuusagetest
Last         : 3230662656
Maximum      : 3231711232
Minimum      : 3225419776
TIMESTAMP    : 10/16/2017 10:00:00 PM
Total        : 774751911936
PartitionKey : *************
RowKey       : ***************__:005CMemory:005CAvailableMemory
Etag         : W/"datetime'2017-10-16T23%3A00%3A05.5000867Z'"

I don't know why there is just TIMESTAMP and another one is Etag! in the result. anyway I can get Timestamp using this command:

$result[0].Timestamp

it will return always an string like 10/16/2017 23:00:00, but in table's row it's stored something like 2017-10-16T23:30:00.000. I need to get exactly the format that it's stored in the storage table.
I tried to change the format using a command like this:

[String]$result[0].Timestamp
or
$result[$j].Timestamp.toString("s")

but the result is same.
Please note When I am using $result[$j].Timestamp.GetType().FullName result is System.DateTime, then the type of Timestamp property in my table is DateTime and I can't (and don't want) to change it.
it's a screenshot of my Azure Storage Explorer that is showing that I have Minute & Second & milliseconds in my Timestamp Above is a screenshot of my Azure Storage Explorer that is showing that I have Minute & Second & milliseconds in my Timestamp

but when I am using this command:

$result[$j].Timestamp.toString("s")

result is without minuets and seconds...
I even test these commands too:

"{0:MM/dd/yyyy hh:mm:ss}" -f $result[$j].Timestamp

but the result is something like 10/16/2017 10:00:00
Can you please guide me?

Upvotes: 0

Views: 1900

Answers (1)

Aaron McGee
Aaron McGee

Reputation: 13

Provided any object of type DateTime d, the command d.ToString("s") will give back the date object in the format 2017-10-16T23:30:00.000

If you have a string and need to convert it to a DateTime, the following will perform a conversion between date strings of different formats:

[DateTime]::Parse("10/16/2017 23:00:00").ToString("s")

With the above evaluating to "2017-10-16T23:30:00.000"

Upvotes: 1

Related Questions