Python Datetime






<!--
main_leaderboard, all: [728,90][970,90][320,50][468,60]
-->



Python Datetime



❮ Previous
Next ❯



Python Dates


A date in Python is not a data type of its own, but we can import a module
named datetime to work with dates as date
objects.



Example


Import the datetime module and display the current date:



import datetime

x = datetime.datetime.now()
print(x)

Run example »




Date Output



When we execute the code from the example above the result will be:






cc = "2018/05/06 09:30:20"
var d = new Date();

var m = d.getMonth() + 1;
if (m < 10) m = "0" + m

var day = d.getDate();
if (day < 10) day = "0" + day;

var h = d.getHours();
if (h < 10) h = "0" + h;

var n = d.getMinutes();
if (n < 10) n = "0" + n;

var s = d.getSeconds();
if (s < 10) s = "0" + s;

var ms = d.getMilliseconds();
while (ms.toString().length < 3) {
ms = "0" + ms;
}
var ex = Math.floor(Math.random() * 999);
while (ex.toString().length < 3) {
ex = "0" + ex;
}

var x = d.getFullYear() + "-" + m + "-" + day + " " + h + ":" + n + ":" + s + "." + ms + ex
document.write(x);


The date contains year, month, day, hour, minute, second, and microsecond.


The datetime module has many methods to return information about the date
object.


Here are a few examples, you will learn more about them later in this
chapter:




Example


Return the year and name of weekday:



import datetime

x = datetime.datetime.now()

print(x.year)
print(x.strftime("%A"))

Run example »




Creating Date Objects



To create a date, we can use the datetime() class (constructor) of the
datetime module.


The datetime() class requires three parameters to create a date: year,
month, day.




Example


Create a date object:



import datetime

x = datetime.datetime(2020, 5, 17)

print(x)

Run example »


The datetime() class also takes parameters for time and timezone (hour,
minute, second, microsecond, tzone), but they are optional, and has a default
value of 0, (None for timezone).









<!--
mid_content, all: [300,250][336,280][728,90][970,250][970,90][320,50][468,60]
-->





The strftime() Method



The datetime object has a method for formatting date objects into readable strings.


The method is called strftime(), and takes one parameter,
format, to specify the format of the returned string:



Example


Display the name of the month:



import datetime

x = datetime.datetime(2018, 6, 1)

print(x.strftime("%B"))

Run example »


A reference of all the legal format codes:

























































































































































DirectiveDescriptionExampleTry it
%aWeekday, short versionWedTry it »
%AWeekday, full versionWednesdayTry it »
%wWeekday as a number 0-6, 0 is Sunday3Try it »
%dDay of month 01-3131Try it »
%bMonth name, short versionDecTry it »
%BMonth name, full versionDecemberTry it »
%mMonth as a number 01-1212Try it »
%yYear, short version, without century18Try it »
%YYear, full version2018Try it »
%HHour 00-2317Try it »
%IHour 00-1205Try it »
%pAM/PMPMTry it »
%MMinute 00-5941Try it »
%SSecond 00-5908Try it »
%fMicrosecond 000000-999999548513Try it »
%zUTC offset+0100
%ZTimezoneCST
%jDay number of year 001-366365Try it »
%UWeek number of year, Sunday as the first day of week, 00-5352Try it »
%WWeek number of year, Monday as the first day of week, 00-5352Try it »
%cLocal version of date and timeMon Dec 31 17:41:00 2018Try it »
%xLocal version of date12/31/18Try it »
%XLocal version of time17:41:00Try it »
%%A % character%Try it »




❮ Previous
Next ❯

Popular posts from this blog

Python Lists

Aion

JavaScript Array Iteration Methods