
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 10476 Articles for Python

4K+ Views
It is very easy to do date and time maths in Python using timedelta objects. Whenever you want to add or subtract to a date/time, use a datetime.datetime(), then add or subtract datetime.timedelta() instances. A timedelta object represents a duration, the difference between two dates or times. The timedelta constructor has the following function signature −datetime.timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])Note: All arguments are optional and default to 0. Arguments may be ints, longs, or floats, and may be positive or negative. You can read more about it here − https://docs.python.org/2/library/datetime.html#timedelta-objectsExampleAn example of using the timedelta objects and dates ... Read More

3K+ Views
In Python, measuring the execution time of a function or block of code can be done using several functions and methods provided by built-in modules like time and datetime. The following are the methods to measure the execution time of Python functions. Using time.time() Method Using time.process_time() Function Using datetime.now() Function Using time.time() Method The time.time() method (returns the current UTC) can measure how long a function takes to run. It returns the current time in seconds since January 1, 1970 (known as the Unix epoch). To ... Read More

8K+ Views
In Python, while working on date and time, converting time seconds to h:m:s (Hours: Minutes: Seconds) format can be done using simple arithmetic operations and built-in modules like datetime and time. The following are several ways to convert time seconds to h:m:s format. Using arithmetic operations (Naive Method) Using the timedelta class of the datetime module Using time.strftime() with time.gmtime() Using arithmetic operations (Naive method) This is the simplest way to convert seconds into time in H:M:S format. We use basic mathematical operations like division and modulo ... Read More

1K+ Views
A millisecond UNIX timestamp is a number that shows how many milliseconds have elapsed since the beginning of the Unix epoch, which is January 1, 1970, at 00:00:00 Coordinated Universal Time (UTC) up to the current moment or specified date and time. Instead of counting time in days, hours, or minutes, it counts in milliseconds (1 second = 1000 milliseconds). In Python, the common way to convert a Datetime object to a milliseconds timestamp involves using the strptime() function (to parse a string into a Datetime object), then converting this datetime object into a UNIX timestamp by using the timestamp() method, ... Read More

266 Views
A timestamp represents a specific point in time as a numerical value. It typically measures the number of seconds that have elapsed since the Unix epoch, which is January 1, 1970, at 00:00:00 Coordinated Universal Time (UTC). The main reasons behind timestamp variations on different machines include differences in time zones, system clocks, locale settings, and the use of UTC and local time (such as IST, PST, or CET). In this article, we will explore timestamp differences across different machines by considering the following key factors. Different System Time Zones If each system were set to different time zones like ... Read More

64K+ Views
The ISO 8601 standard defines an internationally recognised format for representing dates and times. ISO 8601 is a date and time format that helps remove different forms of the day, date, and time conventions worldwide. In this article, we will discuss several methods to get an ISO 8601 date in string format in Python. ISO 8601 Date Format In Python, ISO 8601 date is represented as "YYYY-MM-DDTHH:MM:SS.mmmmmm" format. For example, August 25, 2023, is represented as 2023-08-25T14:35:45.123456. YYYY: Year (four digits) MM: Month (from 1-12) DD: Days (from ... Read More

6K+ Views
Python provides the time and datetime modules to convert a DateTime string into integer milliseconds. Key functions include time.time(), which gives the current time in seconds, and datetime.timestamp(), which converts datetime objects directly into seconds since the epoch. By multiplying these values by 1000, we can get the time in milliseconds. Using time.time() Method The time module in Python provides various methods and functions related to time. Here we use the time.time() method to get the current CPU time in seconds. The time is calculated since the epoch, which returns a floating-point number expressed in seconds. This value is multiplied ... Read More

817 Views
Both Python and JavaScript have unique ways of representing date and time data. To compare Python datetime objects with JavaScript Date objects, we must ensure that both are converted to a common format, such as ISO 8601 strings or Unix timestamps (milliseconds since epoch). The following are two major differences between Python (datetime) and JavaScript (date) objects. Month Representation: JavaScript uses a 0-indexed month (0 for January, 11 for December), while Python uses a 1-indexed month (1 for January, 12 for December). Default Time Zone: Python defaults to UTC, while JavaScript defaults to ... Read More

1K+ Views
In Python, the mktime() function (from the time module) assumes that the passed tuple is in local time, while the calendar.timegm() (from the calendar module) assumes it's in GMT/UTC. Depending on the interpretation, the tuple represents a different time, so both functions return different values (seconds since the epoch are UTC-based). The difference between the values should be equal to the time zone offset of your local time zone. Understanding time.mktime() in Local Time Context The Python time.mktime() method converts the object form of local time into seconds since the epoch (January 1, 1970, 00:00:00 UTC). This method is the inverse function of localtime() and ... Read More

36K+ Views
We can use the datetime module to convert a datetime to a UTC timestamp in Python. If we already have the datetime object in UTC, then the timestamp() function can be directly used to get a UTC timestamp. This function returns the time since epoch for that datetime object. If we have the datetime object in the local timezone, first replace the timezone info and then fetch the time. The following are the various methods to convert a datetime object into a UTC timestamp in Python. Using datetime.timestamp() with UTC-aware datetime Local ... Read More