Unix time (UTC)
2 min readNov 9, 2021
- What is Unix time?
- Why should we store Unix time in the database?
What is Unix time?
- Unix time is the total number of seconds that have been elapsed from
00:00:00 1 January 1970 (GMT) till the current time (GMT).
Unix time = Current time in GMT - 00:00:00 1 January 1970(GMT)
- Unix time is time zone independent.
- Unix time is also known as Epoch time, Posix time, or UNIX Epoch time.
- To check how many seconds have been elapsed currently now — refer to this link.
- Usually, the timestamp and dates are stored in UTC in the database.
To understand better, lets cosider an example, I have written this blog on 11/09/2021:4:30 pm (IST-Indian standard time) and the
UNIX time for 11/09/2021:4:30 pm(IST) is showing 1636455600 seconds.What does 1636455600 seconds actually mean?
Lets analyse below and try to understand more -IST(Indian standard time) = GMT + 5hrs:30min.To find out why IST = GMT + 5hrs:30min, refer this link for different time zones. Different countries have different zones.For example
Singapore time = GMT + 8hrs:00min
New York time = GMT - 5hrs:00minWe know the IST time of writting blog is 11/09/2021:4:30 pm.Unix time = Current time of blog in GMT - 00:00:00 1 January 1970(GMT).We have to find out the GMT time of writting blog i.e: IST time of writing a blog = GMT time + 5hrs:30min.
GMT time = IST of writing blog - 05hrs:30min.
GMT time = 11/09/2021:4:30 pm - 5hrs:30min
GMT time = 11/09/2021: 11:30 am (GMT)Unix time = GMT time(of blog written) - 1 January 1970(GMT).
Unix time = 11/09/2021:11:30am(GMT) - 1 January 1970(GMT)
= 1636455600 seconds.1636455600 seconds means, its the total number of seconds elapsed from 1 January 1970 (GMT) to 11/09/2021:11:30am (GMT).
Why should we store Unix time in the database?
- Browsers will automatically convert from UNIX time to the local time.
We found 1636455600 seconds = 11/09/2021:11:30am (GMT).If we pass new Date(1636455600) in browser(Chrome: inspect mode)Browser will automatically find out the current time zone i.e - IST and it adds GMT + 05hrs:30min autmotically.(If browser was opened in singapore then browser will automatically add 08:00hrs)
(If browser was opened in New York then browser will automatically subtract 05:00hrs)= 11/09/2021:11:30am (GMT) + 05hrs: 30min.
= 11/09/2021:04:30pmIn browser we will see 11/09/2021:04:30pm(GMT+05:30).
- So in this way, if the browser was opened in India, it will show 11/09/2021:04:30 pm. If the browser was opened in New York, it will show 11/09/2021:06:00 am.
Thanks …