-2
distYearMonths: {2018: ["Oct", "Sep", "Dec", "Nov"],…}
2018: ["Oct", "Sep", "Dec", "Nov"]
2019: ["Jan", "Jul", "Jun", "Nov", "Oct", "Sep", "Apr", "Aug", "Dec", "Feb", "Mar", "May"]
2020: ["Feb", "Jan", "Mar", "May", "Apr", "Jun"]

I want to show month numbers instead of names in the same above format. Can anyone suggest to me the solution?

mr. pc_coder
  • 16,412
  • 3
  • 32
  • 54
  • please share also html code how u want to show it. And your variable is not valid object – mr. pc_coder Dec 30 '20 at 08:12
  • simply use a dictionary. @pc_coder already suggested but you can make an object like `const obj = { "Jan": 1, "Feb": 2, "Mar": 3, "Apr": 4, ... }` – canbax Dec 30 '20 at 08:48
  • https://stackoverflow.com/questions/62830215/how-to-get-monthname-from-number-using-angular-datepipe/62830320#62830320 – Eliseo Dec 30 '20 at 08:54

1 Answers1

2

in html to show number rather than name then u can use pipe as a way

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'month'
})
export class MonthpipePipe implements PipeTransform {

  transform(value: any): any {
    return Months[value];
  }

}
enum Months{
  "Jan"=1,
  "Feb"=2,
  "Mar"=3,
  "Apr"=4,
  "May"=5,
  "Jun"=6,
  "Jul"=7,
  "Aug"=8,
  "Sep"=9,
  "Oct"=10,
  "Nov"=11,
  "Dec"=12,
}

then in html write pipe to convert to number in html. Demo

mr. pc_coder
  • 16,412
  • 3
  • 32
  • 54