首页 > 解决方案 > 我想拆分从 API 获得的数据并将其转换为日期和月份

问题描述

这是我从 API 调用数据的地方。

  Center(
  child: Container(
    margin: EdgeInsets.symmetric(vertical: 1.5.h),
    child: Column(
      children: [
        Text(
          data[index].waktu, //this will show "2021/03/11" but all I want is only '03' and convert it to month
          style: TextStyle(
              fontWeight: FontWeight.bold,
              color: Colors.white),
        ),
        Padding(
          padding:
          EdgeInsets.only(top: 1.0.h),
          child: Text(
            data[index].waktu,//this will show "2021/03/11" but all I want is only '11'
            style: TextStyle(
                fontSize: 20.sp,
                fontWeight: FontWeight.bold),
          ),
        ),
      ],
    ),
  ),
),

您能解释一下如何获取我想要的唯一数据并将其转换为月份名称吗?

标签: flutterdart

解决方案


您需要使用DateFormatfrom intl package 来解析您的格式,然后格式化为您需要的格式。

例如,对于您的第一个文本:替换

data[index].waktu,

DateFormat('MMMM').format(DateFormat("yyyy/MM/dd").parse(data[index].waktu)),

推荐阅读