简体   繁体   English

如何在 python 中将 (2018-09-05T09:00:06.540486Z) 转换为 (2018-09-05)?

[英]How To Convert (2018-09-05T09:00:06.540486Z) Into (2018-09-05) In python?

I have searched for a solution but I have not found any, I have a dictionary like This我已经搜索了一个解决方案,但我没有找到任何解决方案,我有一本这样的字典

tmp ={
    'admission_date':adm.admission_date,
    'course':adm.course.name
}

In models.py i have admission_date to DatetimeField .在models.py 中,我有admission_date 到DatetimeField In the Get Function I want to format this date like this 2018-09-05 By removing all part How can i do This.在获取功能中,我想像这样格式化这个日期 2018-09-05 通过删除所有部分我该怎么做。

If you want datetime object, use .date()如果你想要datetime对象,请使用.date()

tmp = {
    'admission_date': adm.admission_date.date(),
    'course': adm.course.name
}


else if you want string representation , use .strftime()否则,如果您想要字符串表示,请使用.strftime()

tmp = {
    'admission_date': adm.admission_date.strftime('%Y-%m-%d'),
    'course': adm.course.name
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Python 中将 yyyy-mm-dd (2018-08-09) 转换为 Aug09? - How do I convert yyyy-mm-dd (2018-08-09) to Aug09 in Python? Python 查找类型并将“2022-07-12T05:09:39.057266+00:00”字符串转换为日期和时间 - Python find the type and Convert "2022-07-12T05:09:39.057266+00:00 "String to date and time 将 2020-09-01T00:00:00-05:00 时间戳转换为 dd-mm-yyyy - Convert 2020-09-01T00:00:00-05:00 timestamp to dd-mm-yyyy 如何使用 pandas 以小时为单位转换 09:20:05 时间格式? - How to convert 09:20:05 time format in hour using pandas? 如何在 python 中将“2021-09-01T14:37:40.537Z”转换为“2021-09-01T14:37:40.5370000+00:00”? - how to convert "2021-09-01T14:37:40.537Z" into "2021-09-01T14:37:40.5370000+00:00" in python? 解析器错误:小时必须在 0..23:09/05/2019 24:00 - ParserError: hour must be in 0..23: 09/05/2019 24:00 UTC 日期时间问题 - 如何适合冒号 - 时间数据“2021-03-11 09:30:01-05:00”与格式“%Y%m%d %H:%M:%S %z”不匹配' - Problems with UTC datetime - how to fit a colon - time data '2021-03-11 09:30:01-05:00' does not match format '%Y%m%d %H:%M:%S %z' 如何在 Pandas 数据框中转换时间 2020-09-25T00:20:00.000Z - How to convert the time 2020-09-25T00:20:00.000Z in pandas dataframe 如何将“2019-11-30T07:00:00+09:00”日期转换为整数? - How to convert "2019-11-30T07:00:00+09:00" date into integer? 如何在 Python 中像往常一样返回输入时间 09:00:00 - How to return the input time as always 09:00:00 in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM