Converting date between DD/MM/YYYY and YYYY-MM-DD?

前端 未结 6 1897
隐瞒了意图╮
隐瞒了意图╮ 2020-12-23 16:24

Using a Python script, I need to read a CVS file where dates are formated as DD/MM/YYYY, and convert them to YYYY-MM-DD before saving this into a SQLite database.

Th

相关标签:
6条回答
  • 2020-12-23 16:55

    Your example code is wrong. This works:

    import datetime
    
    datetime.datetime.strptime("21/12/2008", "%d/%m/%Y").strftime("%Y-%m-%d")
    

    The call to strptime() parses the first argument according to the format specified in the second, so those two need to match. Then you can call strftime() to format the result into the desired final format.

    0 讨论(0)
  • 2020-12-23 16:58

    you first would need to convert string into datetime tuple, and then convert that datetime tuple to string, it would go like this:

    lastconnection = datetime.strptime("21/12/2008", "%d/%m/%Y").strftime('%Y-%m-%d')
    
    0 讨论(0)
  • 2020-12-23 16:58

    In case you need to convert an entire column of data (from pandas DataFrame), then first convert it (pandas Series) to the datetime format using to_datetime and finally use .dt.strftime:

    def conv_dates_series(df, col, old_date_format, new_date_format):
    
        df[col] = pd.to_datetime(df[col], format=old_date_format).dt.strftime(new_date_format)
    
        return(df)
    
    

    Sample usage:

    import pandas as pd
    
    test_df = pd.DataFrame({"Dates": ["1900-01-01", "1999-12-31"]})
    
    old_date_format='%d/%m/%Y'
    new_date_format='%Y-%m-%d'
    
    conv_dates_series(test_df, "Dates", old_date_format, new_date_format)
    
    0 讨论(0)
  • 2020-12-23 17:01

    I am new to programming. I wanted to convert from yyyy-mm-dd to dd/mm/yyyy to print out a date in the format that people in my part of the world use and recognise.

    The accepted answer above got me on the right track.

    The answer I ended up with to my problem is:

    import datetime
    
    today_date = datetime.date.today()
    print(today_date)
    
    new_today_date = today_date.strftime("%d/%m/%Y")
    print (new_today_date)
    

    The first two lines after the import statement gives today's date in the USA format (2017-01-26). The last two lines convert this to the format recognised in the UK and other countries (26/01/2017).

    You can shorten this code, but I left it as is because it is helpful to me as a beginner. I hope this helps other beginner programmers starting out!

    0 讨论(0)
  • 2020-12-23 17:15

    Does anyone else else think it's a waste to convert these strings to date/time objects for what is, in the end, a simple text transformation? If you're certain the incoming dates will be valid, you can just use:

    >>> ddmmyyyy = "21/12/2008"
    >>> yyyymmdd = ddmmyyyy[6:] + "-" + ddmmyyyy[3:5] + "-" + ddmmyyyy[:2]
    >>> yyyymmdd
    '2008-12-21'
    

    This will almost certainly be faster than the conversion to and from a date.

    0 讨论(0)
  • 2020-12-23 17:21
    #case_date= 03/31/2020   
    
    #Above is the value stored in case_date in format(mm/dd/yyyy )
    
    demo=case_date.split("/")
    new_case_date = demo[1]+"-"+demo[0]+"-"+demo[2]
    #new format of date is (dd/mm/yyyy) test by printing it 
    print(new_case_date)
    
    0 讨论(0)
提交回复
热议问题