DATES



Dates - Python


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.


Import the datetime module and display the current date:

import datetime

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

==========o/p========

2019-11-08 15:45:52.021931


Date Output :


When we execute the code from the example above the result will be::'2019-11-08 15:45:52.021931'

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:


turn the year and name of weekday:

import datetime

x = datetime.datetime.now()

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


========o/p=========
2019
Friday



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.


Create a date object:

import datetime

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

print(x)

========o/p========

2019-08-11 00:00:00

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).


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:


Display the name of the month:

import datetime
x = datetime.datetime(2019, 11, 8)
print(x.strftime("%B"))

========o/p========

November


A reference of all the legal format codes:


DirectiveDescription
%aWeekday, short version
import datetime
x=datetime.datetime.now()
print(x.strftime("%a"))

========O/P========
Fri


DirectiveDescription
%AWeekday, full version
import datetime
x=datetime.datetime.now()
print(x.strftime("%A"))

========O/P=========
Friday

DirectiveDescription
%wWeekday as a number 0-6, 0 is Sunday
import datetime
x=datetime.datetime.now()
print(x.strftime("%w"))

========o/p========
5

DirectiveDescription
%dDay of month 01-31
import datetime
x=datetime.datetime.now()
print(x.strftime("%d"))

========o/p=======
08

DirectiveDescription
%bMonth name, short version
import datetime
x=datetime.datetime.now()
print(x.strftime("%b"))

========o/p========
Nov

DirectiveDescription
%BMonth name, full version
import datetime
x=datetime.datetime.now()
print(x.strftime("%B"))

========O/P=========
November

DirectiveDescription
%mMonth as a number 01-12
import datetime
x=datetime.datetime.now()
print(x.strftime("%m"))

======o/p=======
11

DirectiveDescription
%yYear, short version, without century
import datetime
x=datetime.datetime.now()
print(x.strftime("%y"))

=======o/p========
19

DirectiveDescription
%YYear, full version
import datetime
x=datetime.datetime.now()
print(x.strftime("%Y"))

========O/P=========
2019

DirectiveDescription
%HHour 00-23
import datetime
x=datetime.datetime.now()
print(x.strftime("%H"))

========O/P=========
17

DirectiveDescription
%IHour 00-12
import datetime
x=datetime.datetime.now()
print(x.strftime("%I"))

=========O/P=========
05

DirectiveDescription
%pAM/PM
import datetime
x=datetime.datetime.now()
print(x.strftime("%p"))

=========o/p=========
PM

DirectiveDescription
%MMinute 00-59
import datetime
x=datetime.datetime.now()
print(x.strftime("%M"))

========O/P========
31


DirectiveDescription
%SSecond 00-59
import datetime
x=datetime.datetime.now()
print(x.strftime("%S"))

=======O/P========
46

DirectiveDescription
%fMicrosecond 000000-999999
import datetime
x=datetime.datetime.now()
print(x.strftime("%f"))

========o/p========
548204



DirectiveDescriptionExample
%zUTC offset+0100

DirectiveDescriptionExample
%ZTimezoneCST

DirectiveDescription
%jDay number of year 001-366
import datetime
x=datetime.datetime.now()
print(x.strftime("%j"))

=======o/p=======
312

DirectiveDescription
%UWeek number of year, Sunday as the first day of week, 00-53

import datetime
x=datetime.datetime.now()
print(x.strftime("%U"))

========O/P=========
44

DirectiveDescription
%WWeek number of year, Monday as the first day of week, 00-53
import datetime
x=datetime.datetime.now()
print(x.strftime("%W"))

=======O/P=======
44

DirectiveDescription
%cLocal version of date and time
import datetime
x=datetime.datetime.now()
print(x.strftime("%c"))

========o/p========
Fri Nov  8 17:48:11 2019


DirectiveDescription
%xLocal version of date
import datetime
x=datetime.datetime.now()
print(x.strftime("%x"))

========o/p========
11/08/19


DirectiveDescription
%XLocal version of time
import datetime
x=datetime.datetime.now()
print(x.strftime("%X"))

=========O/P=========
17:50:57


DirectiveDescription
%%A % character
import datetime
x=datetime.datetime.now()
print(x.strftime("%%"))

========O/P=========
%


NOTE : All the outputs are displayed according to the present date , time , month , year , hours , minutes , seconds and microseconds (AS WE MENTIONED now( )).



RegEx SETS

Python - RegEx SETS

posted on 2019-11-12 23:06:24 - Python Tutorials


RegEx_Functions

Python - RegEx_Functions

posted on 2019-11-09 06:07:29 - Python Tutorials


RegEx_Sets

Python - RegEx_Sets

posted on 2019-11-09 05:30:54 - Python Tutorials


Prompt Examples

ChatGPT Prompt Examples

posted on 2023-06-21 22:37:19 - ChatGPT Tutorials


Use Cases

Chat GPT Key Use Cases

posted on 2023-06-21 21:03:17 - ChatGPT Tutorials


Prompt Frameworks

Prompt Frameworks

posted on 2023-06-21 19:33:06 - ChatGPT Tutorials