Django日期格式问题 - 格式不符 [英] Django date format issue - does not match format

查看:194
本文介绍了Django日期格式问题 - 格式不符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将日期更改为格式 MM / YY 。问题是,我并不总是以日期格式开始。这是我的理解,下面的代码需要知道什么格式的日期是在第一次,我可以改变它是正确的?

I need to change the date into the format MM/YY. The issue is I don't always don't what the date format is to begin with. It's my understanding that the code below would need to know what format the date was in first before I can change it is that correct?

newdate = datetime.datetime.strptime(str(mydate), '%Y/%m/%d').strftime('%m-%Y')

因此我收到错误:


时间数据'2020- 09-30'不符合格式'%Y /%m /%d'(

time data '2020-09-30' does not match format '%Y/%m/%d' (

如何将我的日期转换为MM / YY?

How to I convert my date to MM/YY?

我正在使用Django 1.4.6

I'm using Django 1.4.6

推荐答案

newdate = datetime.datetime.strptime(str(mydate), '%Y-%m-%d').strftime('%m/%Y')

这篇关于Django日期格式问题 - 格式不符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆