Date and Time


#1

Hello Everyone,

Can anyone please tell me how to print the date and time according to IST?


#2
23:03

Friday, 4 May 2018 (UTC+5:30)

Time in India Standard Time

Running this code in the lesson 6 module for Date and Time

from datetime import datetime
now = datetime.now()

print '%s/%s/%s %s:%s:%s' % (now.month, now.day, now.year, now.hour, now.minute, now.second)

give us the time in UTC

5/4/2018 17:38:14

We see from above that India Standard Time is offset by (UTC+5:30) so just add 5:30 to the UTC time.

If your have a Python interactive console it should draw upon your system time and give you the current time for your locale.

>>> from datetime import datetime
>>> now = datetime.now()
>>> now
datetime.datetime(2018, 5, 4, 11, 37, 37, 718768)
>>> 

In JavaScript we would instantiate a new Date object

 > now = new Date()
<- 2018-05-04T17:44:39.946Z

The above reads in UTC as indicated by the Z which stands for Zulu, the old term used to describe GMT (Greenwich Mean Time)

now = new Date();
console.log(now);
hour = now.getHours();
console.log(hour);
minute = now.getMinutes();
console.log(minute);

Output for my local time:

2018-05-04T17:52:24.792Z
11
52

Note that the now instance reflects UTC time, whereas the hour and minutes values are my local hour and minute (UTC-6:00).

To determine the offset, we can call the getTimezoneOffset() method which returns the number of minutes difference between UTC and local time.

offset = now.getTimezoneOffset();
console.log(offset);
// <- 360

In other words, UTC is our time plus 6 hours. Your result might be something like,

-330    *edit: oops!*

meaning UTC is 5 and a half hours behind your local time.