Python string to datetime yyyy-mm-dd

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In this article, we are going to convert DateTime string of the format ‘yyyy-mm-dd’ into DateTime using Python. 

    yyyy-mm-dd stands for year-month-day .

    We can convert string format to datetime by using the strptime[] function. We will use the  ‘%Y/%m/%d’  format to get the string to datetime.

    Syntax:

    datetime.datetime.strptime[input,format]

    Parameter:

    • input is the string datetime
    • format is the format – ‘yyyy-mm-dd’
    • datetime is the module

    Example: Python program to convert string datetime format to datetime

    Python3

    import datetime

    input = '2021/05/25'

    format = '%Y/%m/%d'

    datetime = datetime.datetime.strptime[input, format]

    print[datetime.date[]]

    Example 2: Convert a list of string DateTime to DateTime

    Python3

    import datetime

    input = ['2021/05/25', '2020/05/25', '2019/02/15', '1999/02/4']

    format = '%Y/%m/%d'

    for i in input:

        print[datetime.datetime.strptime[i, format].date[]]

    Output

    2021-05-25
    2020-05-25
    2019-02-15
    1999-02-04
    


    View Discussion

    Improve Article

    Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In this article, we are going to convert the DateTime string into the %Y-%m-%d-%H:%M:%S format. For this  task strptime[] and strftime[] function is used.

    strptime[] is used to convert the DateTime string to DateTime in the format of year-month-day hours minutes and seconds

    Syntax:

    datetime.strptime[my_date, “%d-%b-%Y-%H:%M:%S”]

    strftime[] is used to convert DateTime into required timestamp format

    Syntax:

    datetime.strftime[“%Y-%m-%d-%H:%M:%S”]

    Here,

    • &Y means year
    • %m  means month
    • %d means day
    • %H means hours
    • %M means minutes
    • %S means seconds.

    First the take DateTime timestamp as a String. Then, convert it into DateTime using strptime[]. Now, convert into the necessary format of DateTime using strftime

    Example 1: Python program to convert DateTime string into %Y-%m-%d-%H:%M:%S format

    Python3

    from datetime import datetime

    my_date = "30-May-2020-15:59:02"

    d = datetime.strptime[my_date, "%d-%b-%Y-%H:%M:%S"]

    print[d.strftime["%Y-%m-%d-%H:%M:%S"]]

    Output

    '2020-05-30-15:59:02'

    Example 2: Python program to convert DateTime string into %Y-%m-%d-%H:%M:%S format

    Python3

    from datetime import datetime

    my_date = "30-May-2020-15:59:02"

    d = datetime.strptime[my_date, "%d-%b-%Y-%H:%M:%S"]

    print[d.strftime["%Y-%m-%d-%H:%M:%S"]]

    my_date = "04-Jan-2021-02:45:12"

    d = datetime.strptime[my_date, "%d-%b-%Y-%H:%M:%S"]

    print[d.strftime["%Y-%m-%d-%H:%M:%S"]]

    my_date = "23-May-2020-15:59:02"

    d = datetime.strptime[my_date, "%d-%b-%Y-%H:%M:%S"]

    print[d.strftime["%Y-%m-%d-%H:%M:%S"]]

    Output

    2020-05-30-15:59:02
    2021-01-04-02:45:12
    2020-05-23-15:59:02
    


    How do you convert string to date in dd

    We can convert string format to datetime by using the strptime[] function. We will use the '%Y/%m/%d' format to get the string to datetime..
    input is the string datetime..
    format is the format – 'yyyy-mm-dd'.
    datetime is the module..

    How do I convert a string to a datetime in Python?

    We can convert a string to datetime using strptime[] function. This function is available in datetime and time modules to parse a string to datetime and time objects respectively.

    How do I convert a string to a timestamp in Python?

    Import the datetime library. Use the datetime. datetime class to handle date and time combinations. Use the strptime method to convert a string datetime to a object datetime.

    What is the difference between Strftime and Strptime in Python?

    strptime is short for "parse time" where strftime is for "formatting time". That is, strptime is the opposite of strftime though they use, conveniently, the same formatting specification.

    Chủ Đề