In my PC, RTC is not working, So I want Date&Time from server using curl command or is there any possibilities to get date and time without RTC in PC. Required format for date&time is DDMMYYYYHHMMSS.

  • I'm curious, why you need to do this using curl? Mar 2, 2016 at 17:49
  • In my PC RTC Not working. But, i want time Stamp in my code. So i use Curl to do that.
    – manikanta
    Mar 5, 2016 at 7:29

5 Answers 5


First, curl makes http(s) requests. Assuming that you have a webserver listening on the machine, so you have 2 options:

  1. Create a script (php/asp/node) that gives you the date and time as response to the request.

  2. Get the time from server response headers.

    curl -v https://google.com/

    this will give you among other things, a line like this

    < Date: Wed, 02 Mar 2016 18:39:13 GMT

    With that result, you can parse and transform to the format that you want.

    dateFromServer=$(curl -v --silent https://google.com/ 2>&1 \
       | grep Date | sed -e 's/< Date: //'); date +"%d%m%Y%H%M%S" -d "$dateFromServer"

    You must note the difference of GMT vs. local time, and adapt it to your needs.

Update 2018-11-26:
As @SopalajodeArrierez noted, it's not possible to run this when the Certificates are not updated or the time is way off the correct value, so the --insecure options is needed on curl command.

    dateFromServer=$(curl -v --insecure --silent https://google.com/ 2>&1 \
       | grep Date | sed -e 's/< Date: //'); date +"%d%m%Y%H%M%S" -d "$dateFromServer"
  • getting like this not working...root@sai:~# curl -v https://google.cl * Rebuilt URL to: https://google.cl/ * Hostname was NOT found in DNS cache * Could not resolve host: google.cl * Closing connection 0 curl: (6) Could not resolve host: google.cl
    – manikanta
    Feb 10, 2017 at 11:36
  • what about using https://google.com/
    – bistoco
    Feb 10, 2017 at 19:12
  • 1
    Not possible to run this curl command from some wrong date's computer (i.e: date at 2006 year): there will be a SSL certificate problem: certificate is not yet valid. In this case, I have managed to solve it with the -k (--insecure) option. Nov 26, 2018 at 4:39
  • I fixed one of the answers: dateFromServer=$(curl -v --silent https://nist.time.gov/ 2>&1 \ | grep '< Date' | sed -e 's/< Date: //'); date +"%d%m%Y%H%M%S" -d "$dateFromServer" but it would be better to use: dateFromServer=$(curl -v --silent https://nist.time.gov/ 2>&1 \ | grep '< Date' | sed -e 's/< Date: //'); date +"%Y-%m-%d %H:%M:%S" -d "$dateFromServer" for sorting purposes (eventually).
    – pbies
    Jun 4 at 1:42

If you must get the time from a remote server, use an NTP server.

For example:

$ ntpq -c 'rv 0 clock' localhost
clock=da8b6fe7.ae195287  Thu, Mar 10 2016  9:30:39.680

The clock value is the an NTP timestamp in hex (the epoch being 01/01/1900). You can process it a bit to get an Unix timestamp which you can then use with date:

$ ntpq -c raw -c 'rv 0 clock' localhost
Output set to raw
$ ntpq -c raw -c 'rv 0 clock' localhost | gawk -F '[ =.]' --non-decimal-data 'NR==2{printf "@%d.%d\n", $2 - 2209075200, "0x"$3}'
$ ntpq -c raw -c 'rv 0 clock' localhost | gawk -F '[=.]' --non-decimal-data 'NR==2{printf "@%d.%d\n", $2 - 2209075200, "0x"$3}' | xargs date +'%d%m%Y%H%M%S' -d 

In my case, I have an NTP daemon running locally, which I could query, hence the usage of localhost. You could use, instead, one of the many public NTP servers, such as ntp.ubuntu.com.

The awk command reads the line, splits it on = and ., so that the three fields are clock, the integer part of the timestamp and the fractional part. Then, it subtracts 2209075200 (the difference in seconds between the Unix and NTP epochs, as obtained from this SO post) from the integer part and prints both the parts in decimal.


Get the date from a HTTP response header. Remove clutter. Set the date.

$ date -s `curl -I 'https://google.com/' 2>/dev/null | grep -i '^date:' | sed 's/^[Dd]ate: //g'`

Hope it helps.

  • 1
    Alternatively if you just want to print: date -d "$(curl -sI google.com| grep -i '^date:'|cut -d' ' -f2-)" also when I tested, google.co was really offset : ~20mins. others where ok though. For Info I use this to check if my VM is ntp desync (in addition of chrony checks)
    – Boop
    Oct 28, 2019 at 10:17

Here's how I do it. I keep a switch statement for translating from the three character month representation returned from a server to the digit value of the month. This should work on many standard Unix environments, as long as curl, echo, cut, date, and bash are installed.

    # Automatically Updates System Time According to the NIST Atomic Clock in a Linux Environment
    nistTime=$(curl -I --insecure 'https://nist.time.gov/' | grep "Date")
    echo $nistTime
    dateString=$(echo $nistTime | cut -d' ' -f2-7)
    dayString=$(echo $nistTime | cut -d' ' -f2-2)
    dateValue=$(echo $nistTime | cut -d' ' -f3-3)
    monthValue=$(echo $nistTime | cut -d' ' -f4-4)
    yearValue=$(echo $nistTime | cut -d' ' -f5-5)
    timeValue=$(echo $nistTime | cut -d' ' -f6-6)
    timeZoneValue=$(echo $nistTime | cut -d' ' -f7-7)
    #echo $dateString
    case $monthValue in
    date --utc --set $yearValue.$monthValue.$dateValue-$timeValue

use timestamp in htttp reponse header.

date -d "$(curl -sI baidu.com| grep -i '^date:'|cut -d' ' -f2-)"
date -d "$(curl -sI baidu.com| grep -i '^date:'|cut -d' ' -f2-)" | xargs -I {} date --date='{}' "+%Y-%m-%d %H:%M:%S"
# below cmd is set new date
date -s "$(date -d "$(curl -sI baidu.com| grep -i '^date:'|cut -d' ' -f2-)" | xargs -I {} date --date='{}' "+%Y-%m-%d %H:%M:%S")"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.