Change language

Python | Convert string to DateTime and vice versa

|

A program for converting a string to DateTime using the strptime () function.

Examples :

  Input:  Dec 4 2018 10:07 AM  Output:  2018-12-04 10:07:00  Input:  Jun 12 2013 5 : 30PM  Output:  2013-06-12 17:30:00 

strptime () is available in the date and time modules and is used to convert time. This function changes the given date and time string to the desired format.

Syntax :

 datetime.strptime (date_string, format) 

The date_string and format arguments must be of type string.

import datetime

 
# Function for converting a string to date and time

def convert (date_time):

format = ’% b% d % Y% I:% M% p’ # Format

datetime_str = datetime.datetime. strptime (date_time, format )

 

return datetime_str

 
# Driver code

date_time = ’Dec 4 2018 10:07 AM’

print (convert (date_time))

Exit :

 04/18/2018 10:07:00 AM 

Program for converting DateTime to string
Examples :

  Input:  2018-12-04 10:07:00  Output:  Dec 4 2018 10:07:00 AM  Input:  2013-06-12 17: 30: 00Jun 12 2013 5:30 PM  Output:  Jun 12 2013 5:30:00 PM 
< p> Python function strftime () is present in the datetime and time modules to create a string representation based on the specified format string.

Syntax :

datetime_object.strftime(format_str)

Another similar function is available in a temporary unit that converts a tuple object or struct_time to a string as specified by the format argument.

import time

  
# Function to convert string to date and time

def convert (datetime_str):

datetime_str = time.mktime (datetime_str)

  

  format = "% b% d% Y% r" # Format

dateTime = time.strftime ( format , time.gmtime (datetime_str))

  return dateTime

  
# Driver code

date_time = ( 2018 , 12 , 4 , 10 , 7 , 00 , 1 , 48 , 0 )

print (convert (date_time))

Exit :

 Dec 04 2018 10:07:00 AM 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically