Skip to main content

Get current date using Python

Prerequisite: DateTime module

In Python, Date and Time are not data types of their own, but a module named DateTime can be imported to work with the date as well as time. Datetime module comes built into Python, so there is no need to install it externally. The DateTime module provides some functions to get the current date as well as time.

Get the current date using date.today()

The today() method of date class under DateTime module returns a date object which contains the value of Today’s date.

Syntax: date.today() 

Returns: Return the current local date.

Example: 

Python3




# Import date class from datetime module
from datetime import date
 
# Returns the current local date
today = date.today()
print("Today date is: ", today)

Output:

Today date is:  2019-12-11

Get the current date using datetime.now()

Python library defines a function that can be primarily used to get the current time and datetime.now() function and return the current local date and time, which is defined under the DateTime module.

Syntax: datetime.now(tz) 

Parameters : tz : Specified time zone of which current time and date is required. (Uses Greenwich Meridian time by default.) Returns : Returns the current date and time in time format.

Example:

Python3




# Import datetime class from datetime module
from datetime import datetime
 
# returns current date and time
now = datetime.now()
print("now = ", now)

Output:

now =  2019-12-11 10:58:37.039404

Attributes of now()

The now() has different attributes, same as attributes of time such as year, month, date, hour, minute, and second. 

Example

Python3




# importing datetime module for now()
import datetime
     
# using now() to get current time
current_time = datetime.datetime.now()
     
# Printing attributes of now().
print ("The attributes of now() are : ")
     
print ("Year: ", end = "")
print (current_time.year)
     
print ("Month: ", end = "")
print (current_time.month)
     
print ("Day: ", end = "")
print (current_time.day)

Output:

The attributes of now() are: 
Year: 2019
Month: 12
Day: 11

RECOMMENDED ARTICLES – Get Current Date and Time using Python

https://neveropen.tech/get-current-date-using-python/?feed_id=89&_unique_id=683cf4a436299

Comments

Popular posts from this blog

Bare Metal Billing Client Portal Guide

Contents Order a Bare Metal Server My Custom / Contract Pricing View Contract Details Location Management Order History & Status View Order Details Introduction The phoenixNAP Client Portal allows you to purchase bare metal servers and other phoenixNAP products and services. Using the intuitive interface and its essential tools, you can also easily manage your infrastructure. This quick guide will show you how to use the new form to order a bare metal server and how to navigate through new bare metal features within the phoenixNAP Client Portal. Order a Bare Metal Server An order form is an accordion-based process for purchasing phoenixNAP products. Our order form allows you to view the pricing and order multiple products from the same category at the same time. Note: The prices on the form are per month . A contract is not required. However, if you want a contracted price, you may be eligible for a discount depending on the quantity and ...

Add an element in Array to make the bitwise XOR as K

Given an array arr[] containing N positive integers, the task is to add an integer such that the bitwise Xor of the new array becomes K. Examples: Input: arr[] = 1, 4, 5, 6, K = 4 Output: 2 Explanation: Bit-wise XOR of the array is 6.  And bit-wise XOR of 6 and 2 is 4. Input: arr[] = 2, 7, 9, 1, K = 5 Output: 8   Approach: The solution to the problem is based on the following idea of bitwise Xor: If for two numbers X and Y , the bitwise Xor of X and Y is Z then the bitwise Xor of X and Z is Y. Follow the steps to solve the problem: Let the bitwise XOR of the array elements be X .  Say the required value to be added is Y such that X Xor Y = K . From the above observation, it is clear that the value to be added (Y) is the same as X Xor K . Below is the implementation of the above approach: C++ // C++ code to implement the above approach   #include using namespace std;   // Function to find the required value int find_...

Mahotas – Template Matching

In this article we will see how we can do template matching in mahotas. Template is basically a part or structure of image. In this tutorial we will use “lena” image, below is the command to load it.   mahotas.demos.load('lena') Below is the lena image      In order to do this we will use mahotas.template_match method Syntax : mahotas.template_match(img, template) Argument : It takes image object and template as argument Return : It returns image object    Note : Input image should be filtered or should be loaded as grey In order to filter the image we will take the image object which is numpy.ndarray and filter it with the help of indexing, below is the command to do this   image = image[:, :, 0] Below is the implementation    Python3 # importing required libraries import mahotas import mahotas.demos from pylab import gray, imshow, show import numpy as np import matplotlib.pyplot as plt      # loading image ...