Find Out The Time Since Unix Epoch for a Certain Date Time

Find out the time since unix epoch for a certain date time?

Using date thus:

date --date="Oct 1 09:00:00 BST 2009" +%s

Yields:

1254384000

How to get time in milliseconds since the unix epoch in Javascript?

Date.now() returns a unix timestamp in milliseconds.

const now = Date.now(); // Unix timestamp in millisecondsconsole.log( now );

Get current time in seconds since the Epoch on Linux, Bash

This should work:

date +%s

Generate UNIX time for certain date in Python

To get seconds since the epoch for a given date object, you can create a datetime object from the date's timetuple (hour, minutes, seconds = 0), set the time zone if required and call the timestamp() method:

from datetime import date, datetime, timezone

unix_time = datetime(*date.today().timetuple()[:6], tzinfo=timezone.utc).timestamp()
# 1603929600.0 for 2020-10-29

For the example, I use date.today() which you can replace with any other date object. You can obtain the date object of any datetime object using datetime_object.date().

Note: I'm using tzinfo=UTC here, which is arbitrary / assumes that the input date also refers to UTC. Replace with the appropriate time zone object if needed; for that see zoneinfo. To exactly mimic your javascript code snippet, set tzinfo=None.

Conversion from UNIX time to timestamp starting in January 1, 2000

The time 1 January 1970 00:00:00 is considered the UNIX epoch. So, if you want to convert from UNIX time to a timestamp having an epoch of January 1, 2000 (Let's say, 2000 epoch) the simplest way would be to simply subtract the UNIX time of January 1, 2000 from the UNIX time.

<2000 time> = <UNIX time> - <January 1, 2000 UNIX time>

<UNIX time> = <2000 time> + <January 1, 2000 UNIX time>

Where January 1, 2000 UNIX time is 946684800.

EDIT: The docs does say

Absolute time is measured in seconds from midnight, 1st January 2000.

So, 946684800 is the exact time difference which should be used to calculate. The few seconds difference that you calculated could be attributed to network delay or some other delays.

Get epoch for a specific date using Javascript

You can create a Date object, and call getTime on it:

new Date(2010, 6, 26).getTime() / 1000

python: convert date timestamp to epoch unix time and figure out number of days remaining?

time.strptime to the rescue! Use the format string %Y-%m-%d %H:%M:%S.%f. For example:

   import time
t = '2014-08-14 20:01:28.242'
ts = time.strptime(t, '%Y-%m-%d %H:%M:%S.%f')
timestamp = time.mktime(ts)

Now to convert it to a datetime (from: How do you convert a Python time.struct_time object into a datetime object? ):

   from datetime import datetime
dt = datetime.fromtimestamp(timestamp)

UNIX Time to Date and Time Conversion Problem Java

You created your own code. That probably was the intent of the homework.
Now pinpoint the problem.

For that you can actually create a loop generating different Unix time values.
For each of these values calculate the date using your code, in parallel calculate the date using java.util.Date or java.util.Instant. Compare the dates and print results suitable so you can pinpoint the situations where your code produces deviations. Try to understand the deviations and improve your code.

With that you would not only exercise coding date calculations but also automated testing or test driven development.



Related Topics



Leave a reply



Submit