How To Print Current Date and Time In Linux and Windows From Command Line? – POFTUT

How To Print Current Date and Time In Linux and Windows From Command Line?


Getting current date-time information is very different for different systems and programming languages. We will look at this simple but big issue for the vast majority of operating systems and programming languages.

Linux/FreeBSD/Unix

We can date information from the GUI panel clock but we will look for the command line. We will issue date command to get the current date and time. Linux date format has a lot of options. Alternatively, we can call these as Unix date or bash date examples.

$ date
Linux FreeBSD Unix date and time with Different Formats
Linux FreeBSD Unix date and time with Different Formats

Only Hour and Minute

We can provide parameters to get only time partition from date command.

$ date +%H:%M

Year Month Day Format

We can get current year month day like below

$ date +%Y_%m_%d

Year-Month-Day Format

We can use ready to use parameters like +%F which will list year-month-day

$ date +%F

Month/Day/Year Format

We can use +%D to get a date according to locale settings

$ date +%D

Display Date and Time In Windows Command Line

Windows operating system provides date and time commands in order to print and set current date and time. After issuing the command date and time the current date and time will be displayed and then we will be asked for a new date and time. We can skip this just by pressing Enter or simply do not entering any value.

> date

> time
Display Date and Time In Windows Command Line
Display Date and Time In Windows Command Line

Python Datetime Module and Functions

Python is a scripting language and to get current date time information python function in a code file can be used. An alternative is python interpreter. In this example, we will use an interpreter.

from datetime import datetime 
now=datetime.now()            
now.strftime("%A, %d. %B %Y %I:%M%p")
Python Datetime Module and Functions
Python Datetime Module and Functions

We can get time information with %I:%M format string parameters.

now.strftime("%I:%M")

We get the year, month and day as a string like below

now.strftime("%Y-%m-%d")
Python Datetime Module and Functions
Python Datetime Module and Functions

LEARN MORE  Introduction To Memcached with Telnet and Python

Leave a Comment