Showing posts with label datetime. Show all posts
Showing posts with label datetime. Show all posts

Tuesday, February 1, 2022

Date and time in python using datetime modules

 



import datetime

year_number=2021
month_number=12
date_number=27
hours_number=15
minutes_number=50
seconds_number=30

datetime.datetime(year_number, month_number, date_number, hours_number, minutes_number, seconds_number)



# =============================================================================
#
# =============================================================================

import datetime

print (datetime.date.today())


# =============================================================================
#
# =============================================================================

from datetime import date

print (date.today())





# =============================================================================
#
# =============================================================================

from datetime import date

print (datetime.date.today().day)

print (datetime.date.today().month)

print (datetime.date.today().year)

#If you prefer to write in one sentence, write it as

print (datetime.date.today().day,datetime.date.today().month,datetime.date.today().year)




# =============================================================================
#
# =============================================================================

from datetime import date

now = date.today()

print (now.day)

print (now.month)

print (now.year)

#You can also display the previous 3 sentences in one sentence

print (now.day, now.month, now.year)

#This code is cleaner and compact. Try to write the code in shortest way possible without affecting readability.


# =============================================================================
#
# =============================================================================

from datetime import date

now = date.today()

print (now.weekday())



# =============================================================================
#
# =============================================================================

Today = datetime.now()

print (Today.strftime("%a, %B, %d, %y"))





# =============================================================================
#
# =============================================================================

import datetime

Today = datetime.now()

print ("Today’s date is" ,Today.strftime("%c"))



# =============================================================================
#
# =============================================================================

from datetime import date,time,datetime,timedelta

Time_gap = timedelta(hours = 23, minutes = 34)
#Time_gap is the timedelta object where we can do different calculations on it.

print ("Future time is ", str(datetime.now() + Time_gap))



Saturday, December 25, 2021

date time in python

 # ========================================================================

# date time using python

# ========================================================================


#uncomment to import library 

import pandas as pd 

import datetime as dt


#converting string into datetime 

df1 = pd.DataFrame(

     {

      "DateTime": pd.to_datetime(["2021-11-13 13:45:33",

                                  '2021-11-14 14:13:25',

                                  "2021-11-15 14:39:25", 

                                  "2021-11-16 15:16:43", 

                                  "2021-11-17 16:51:19"]),

     }

)


#split datetime into individual date and time df column

df1['Dates'] = pd.to_datetime(df1['DateTime']).dt.date

df1['Time'] = pd.to_datetime(df1['DateTime']).dt.time


#join date and time columns into a datetime column 

df1['DateTime_join'] = pd.DataFrame(

    pd.to_datetime(df1['Dates'].astype(str) + ' ' + df1['Time'].astype(str)), 

    columns=['Datetime_join']) 

print(df1)



# ========================================================================

# date time format change

# ========================================================================

import pandas as pd

dt = "02-11-2021"

dt1 = pd.to_datetime(dt)

dt1

#output# Timestamp('2021-02-11 00:00:00')

dt2 = pd.to_datetime(dt, format = "%d-%m-%Y")

dt2

#output# Timestamp('2021-11-02 00:00:00')



# ========================================================================

# date time format

# ========================================================================


import pandas as pd

dt = pd.to_datetime("02-11-2021")

dt3 = dt.strftime('%b/%d/%Y')

dt3

#output# 'Feb/11/2021'



python class topic video