Formatting DateTime Object to String

  • Share this:

Code introduction


This function takes a datetime object and returns a string formatted as YYYY-MM-DD HH:MM:SS.


Technology Stack : datetime

Code Type : Function

Code Difficulty : Intermediate


                
                    
import datetime

def format_date(date_obj):
    """
    将datetime对象格式化为字符串。
    """
    return date_obj.strftime("%Y-%m-%d %H:%M:%S")                
              
Tags: