Linux – How to change time zone in a bash script

datelinux

I am working on Linux Ubuntu, and I want a bash script whose output is to convert the timezone 7 hours in advance from my server time.

My server time:

Mon Jul 23 23:00:00 2017

What I want to achieve:

Mon Jul 24 06:00:00 2017

I have tried this one in my bash script:

#!/bin/bash

let var=$(date +%H)*3600+$(date +%M)*60+$(date +%S)
seven=25200
time=$(($var+$seven))

date=$(date --date='TZ="UTC+7"' "+%Y-%m-%d")
hours=$(date -d@$time -u +%H:%M:%S)

echo "$date" "$hours"

the output was:

2017-07-23
06:00:00

The hours works, but the date still matches the server date. Is there another way to solve this?

Best Answer

Taking your question literally, if you just want to get a date string for 7 hours later than the current time in the current zone, that's easy:

date -d "7 hours" "+%Y-%m-%d %H:%M:%S"

If what you're really wanting to do is pull the local date/time in some other timezone, though, then you'd be better off following the advice in some of the other answers.

Related Question