简体   繁体   English

如何将字符串类型的日期转换为另一种日期格式。 | Flutter | Dart

[英]How to Convert date in string type to another date format. | Flutter | Dart

I have a string date and I want to convert it in another format.我有一个字符串日期,我想将其转换为另一种格式。 For eg: I have a date "25/04/2020" as string and I want to convert this to the string as "25 Apr 2020" like this.例如:我有一个日期"25/04/2020"作为字符串,我想像这样将它转换为字符串"25 Apr 2020" "10/02/2020" become "10 Feb 2020", "27/11/2021" become "27 Nov 2021" , like this. "10/02/2020"变成"10 Feb 2020", "27/11/2021"变成"27 Nov 2021" ,像这样。

Use package intl使用intl

import 'package:intl/intl.dart';

void main() {
  var dateString = "25/04/2020";
  var dateTime = DateFormat('dd/MM/yyyy').parse(dateString);
  print(DateFormat('dd MMM yyyy').format(dateTime));
}

Try below code hope its help to you.Used intl package here试试下面的代码希望它对你有帮助。在这里使用 intl package

DateTime now = DateTime.now();
String formattedDate = DateFormat('dd MMM yyyy ').format(now);
print(formattedDate);

Your Widget:您的小部件:

             Text(
                formattedDate,
              ),

Your result screen->您的结果屏幕-> 在此处输入图像描述

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM