Search code examples
c#angularngx-bootstrap

Get Dates from C# API to Angular for ngx-bootstrap DateTimePicker


For using Angular's ngx-bootstrap datepicker Disabled days for the calendar I have a controller on C#

public List<DateTime> DisabledDays(int carId)
{
    var disabledDates = new List<DateTime>();
    var result = GetAll();
    var startResult = _rentalDal.GetAll().Where(x => x.CarId == carId).Select(x => x.RentDate).ToList();
    var endResult = _rentalDal.GetAll().Where(x => x.CarId == carId).Select(x => x.ReturnDate).ToList();

    for (int i = 0; i < result.Data.Count; i++)
    {
        //for (var disDate = startResult[i]; disDate <= endResult[i]; disDate = DateTime.Parse(disDate.AddDays(1).ToString("dd/MM/yyyy")))
        for (var disDate = startResult[i]; disDate <= endResult[i]; disDate = disDate.AddDays(1))
        {
            disabledDates.Add(DateTime.Parse(disDate.ToString("dd/MM/yyyy")));
            //disabledDates.Add(Convert.ToDateTime(disDate)).ToString("MM-dd-yyyy");
            //disabledDatesString.Add(dt.(ToString("d MMM YY")));
        }
    }

    return disabledDates;
}

it's working ok but the format should be like YYYY-MM-DD I tried something but couldn't put output for Angular needed. My Output is

    "2021-01-01T00:00:00",
    "2021-01-02T00:00:00",
    "2021-01-03T00:00:00",
    "2021-01-04T00:00:00",
    "2021-01-05T00:00:00",
    "2021-01-06T00:00:00",
    "2021-01-07T00:00:00",
    "2021-01-08T00:00:00",
    "2021-01-09T00:00:00",
    "2021-01-10T00:00:00",

angulars method works when I put this manuely it works but when I tried to get it with a function its receiving data like above.

    disabledDates: Date[];
    
    this.disabledDates = [
          new Date('2021-03-20'),
          new Date('2021-03-10'),
          new Date('2021-03-11'),
        ];

getDisabledDates(carId:number){
   this.rentalService.getDisabledDates(carId).subscribe(response=>{
    this.disabledDates= response
     console.log(response)
   })

is there a way to get my date format like YYYY-MM-DD like disabledDates manual feed? and the second one is this new Date simply added by a for or for each loop


Solution

  • Since your startResult and endResult variables are List<DateTime> then the modified method will definitely work for you.

    So, you don't need to List<string> as your method's return type instead of List<DateTime>.

    Because at your JSON response both DateTime and string will be string.

    Solution:

    public List<string> DisabledDays(int carId)
    {
        var disabledDates = new List<string>();
        var result = GetAll();
        var startResult = _rentalDal.GetAll().Where(x => x.CarId == carId).Select(x => x.RentDate).ToList();
        var endResult = _rentalDal.GetAll().Where(x => x.CarId == carId).Select(x => x.ReturnDate).ToList();
    
        for (int i = 0; i < result.Data.Count; i++)
        {
            for (var disDate = startResult[i]; disDate <= endResult[i]; disDate = disDate.AddDays(1))
            {
                disabledDates.Add(disDate.ToString("yyyy-MM-dd"));
            }
        }
    
        return disabledDates;
    }