如何将24/01/2023转换为2023-01-24。

huangapple go评论70阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年1月9日 19:54:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056897.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定