英文:
How to convert 24/01/2023 into 2023-01-24
问题
我尝试通过以下代码来转换日期:
DateFormat('yyyy-MM-dd')
.add_yMMMd()
.parse((state as SlotRequestPopUpDataLoadedState).date)
.toString(),
但它抛出错误:
Unhandled Exception: FormatException: Trying to read - from 09/01/2023 at position 3
英文:
I tried converting the date by doing the below code
DateFormat('yyyy-MM-dd')
.add_yMMMd()
.parse((state as SlotRequestPopUpDataLoadedState).date)
.toString(),
);
But it throws error
Unhandled Exception: FormatException: Trying to read - from 09/01/2023 at position 3
答案1
得分: 1
你遇到这个问题是因为你尝试解析一个包含“-”的日期,但你的日期包含“/”。假设这是你的日期:
var date = '24/01/2023';
你可以这样转换它:
var Dtime = DateFormat('dd/MM/yyyy').parse(date);
print('newDate = ${DateFormat('yyyy-MM-dd').format(Dtime)}'); //newDate = 2023-01-24
英文:
You have this issue because you are trying to parse a date that contains -
but your date contains /
. Let's say this is your date:
var date = '24/01/2023';
you can convert it like this:
var Dtime = DateFormat('dd/MM/yyyy').parse(date);
print('newDate = ${DateFormat('yyyy-MM-dd').format(Dtime)}'); //newDate = 2023-01-24
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论