Last active
August 27, 2019 12:32
-
-
Save kpatnayakuni/47a0eebd78729632510780f05a9850bd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| This script returns the current date time from http://worldclockapi.com/ using REST API service. | |
| You can find the latest uri from the site above. | |
| Eastern Standard Time http://worldclockapi.com/api/json/est/now | |
| Coordinated Universal Time http://worldclockapi.com/api/json/utc/now | |
| Also supports JSONP | |
| Central European Standard Time http://worldclockapi.com/api/jsonp/cet/now?callback=mycallback | |
| #> | |
| # utc time url | |
| [string] $WorldClockAPIUrl = 'http://worldclockapi.com/api/json/utc/now' | |
| # Invoke Get method. The API returns the output in json format, but by default Invoke-RestMethod will convert from JSON to readable format (pacustomobject) | |
| [psobject] $ApiResult = Invoke-RestMethod -Method Get -Uri $WorldClockAPIUrl | |
| <# Selecting only current datetime from the api output | |
| $id : 1 | |
| currentDateTime : 2019-02-27T11:51Z | |
| utcOffset : 00:00:00 | |
| isDayLightSavingsTime : False | |
| dayOfTheWeek : Tuesday | |
| timeZoneName : UTC | |
| currentFileTime : 131957418910000000 | |
| ordinalDate : 2019-58 | |
| serviceResponse : | |
| #> | |
| [string] $UTCTimeString = $ApiResult.currentDateTime | |
| # Convert the string to datetime using .Net datetime class method Parse(), and returns datetime in default culture | |
| [datetime]$DateTime = [System.DateTime]::Parse($UTCTimeString) | |
| # output datetime | |
| return $DateTime |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment