首页 > 解决方案 > how to get only day number from date in flutter

问题描述

I am trying to get day from date, and date comes from the api response, api response look like this

 "data": [
{
    _id: 6116c2e12760f71630342604,
    username: 'abc',
    Date: '2021-08-13',
   
  },
  {
    _id: 6119ba9162069c32ccdf11c3,
    username: 'acv',
    Date: '2021-08-15',
    
  }
]

i am trying to do it like this

date=Response.data['data']['Date']);  //if i get date here 2021-08-15 
day=date.split('-') //then will split it [2021,08,05]
day=date[2] //and then will get the day 05

but it is not working, it sends this error

type 'String' is not a subtype of type 'int' of 'index'

when i print this line day=Response.data['data']['Date']); it send same error, it is not getting date, but when i print onResponse.data it prints the api response.

please help how to do this

标签: flutterdatesplit

解决方案


Let's try or you will use day[2]

day=Response.data['data']['Date']);  //if i get date here 2021-08-15 
day=date?.split('-').last; //then will split [05]
print(day) //and then will get the day 05

推荐阅读