none
PowerShell - How to set time zone to collect time? RRS feed

  • Question

  • When I run following code to retrieve the date and time

    (Get-Date).ToUniversalTime(), it returns Wednesday, 16 August, 2017 12:01:37 AM,

    but my local time is Wednesday, 16 August, 2017 8:01:37 AM

    I would like to know on how to set date and time, so it would return the correct time zone for time and return previous day Tuesday, 15 August, 2017 8:01:37 AM instead of Wednesday, 16 August, 2017 8:01:37 AM.

    Does anyone have any suggestions?
    Thanks in advance for any suggestions


    Thanks in advance for any suggestions

    Wednesday, August 16, 2017 12:11 AM

Answers

All replies

  • R.click on the clock in task bar,adjust date/time,locate internet time,update..If a pc OS gives faulty date/time,

    then usually its caused by a faulty MB battery (desktop),replace the battery,most are a 2032 type.Intel says,

    power-off pc,remove battery,wait 10 minutes,install new.Once thru,power-up pc,enter the BIOS,reset

    date/time,reset for add-on hardware,save & exit.

    Wednesday, August 16, 2017 12:24 AM
  • Get-Date - returns local time.  Why are you converting it to UTC?

    You cannot change your clock to fix this.  You local clock will run in the time zone you are in.  Changing it puts you in the wrong TZ and screws up everything to do with dates and times.

    Read this to learn what UTC is and how to use it: https://en.wikipedia.org/wiki/Coordinated_Universal_Time

    By the way.  UTC is called "Coordinated Universal Time" or just "Universal Time".

    If you are asking how to get local time as a string then do this:

    (Get-Date).ToString()
    Get-Date -Format D

    https://msdn.microsoft.com/library/system.globalization.datetimeformatinfo.aspx

    If you are going to work with computers it is critical that you understand dates and times.  They will be an issue as long as you work in the industry.


    \_(ツ)_/

    Wednesday, August 16, 2017 12:44 AM
  • (Get-Date).ToString()

    Get-Date -Format D


    \_(ツ)_/

    I would like to know on how to retrieve the previous day instead of current day.

    Do you have any suggestions?
    Thanks, to everyone very much for any suggestions (^v^)


    Thanks in advance for any suggestions

    Wednesday, August 16, 2017 1:21 AM
  • [datetime]::Today.AddDays(-1)

    Which has nothing at all to do with universal time.


    \_(ツ)_/

    Wednesday, August 16, 2017 1:36 AM
  • I would like to know on how to set following if statement based on following condition:

    If current time > 9 am then

    (Get-Date).ToString()

    else

    [datetime]::Today.AddDays(-1)

    end if

    Do you have any suggestions?
    Thank you very much for any suggestions (^v^)


    Thanks in advance for any suggestions

    Wednesday, August 16, 2017 2:19 AM
  • if([datetime]::Now -gt [datetime]::Today.AddHours(9)){
         # ...

    You would not be asking these questions if you could learn PowerShell.


    \_(ツ)_/

    Wednesday, August 16, 2017 2:44 AM