0

Expected Output: 01-2019 or 02-2019 in string.

I need this in my Angular .ts file. Not in the HTML view file.

Abhijit Mondal Abhi
  • 1,364
  • 4
  • 15
  • 34
  • Refer this https://stackoverflow.com/questions/22962468/angularjs-display-current-date – Code_maniac Jan 29 '19 at 07:39
  • @Code_maniac but I need it in my .ts file. Not in the html. – Abhijit Mondal Abhi Jan 29 '19 at 07:41
  • Possible duplicate of [how to display current month, year in javascript using div](https://stackoverflow.com/questions/12757911/how-to-display-current-month-year-in-javascript-using-div) and [How to get year/month/day from a date object?](https://stackoverflow.com/questions/2013255) – adiga Jan 29 '19 at 07:42
  • Yes I've searched that. I can do that. But, when I am going to format the date in my Ionic Angular .ts file, it doesn't work as my regular way. @adiga – Abhijit Mondal Abhi Jan 29 '19 at 07:47
  • What doesn't work? Get the month, Get the year, add a hyphen in between – adiga Jan 29 '19 at 07:49
  • @adiga But I need 01,02 for single digit month. And I am expecting much more pimplier method, where I don't need to concatenate the strings. – Abhijit Mondal Abhi Jan 29 '19 at 07:54
  • 3
    Mate, every possible combination of date format has been asked on stackoverflow. `dd-mm-yyyy` isn't something new. Just google and make an attempt before asking a question – adiga Jan 29 '19 at 07:57

3 Answers3

16

You can use Date():

this.curdate = (new Date().getMonth() + 1).toString() + '-' + new Date().getFullYear().toString();

Note that new Date().getMonth() start from 0 to 11 so you need to +1 to make it 1 to 12.

Update:

To add leading 0 based on related post you can do: '0' + (new Date().getMonth() + 1).toString().slice(-2)

Explanation:

Since '0' is not a number but a string when you add (+) with another string then it would be concatenated. Then .slice(-2) gives us the last two characters of the string. If it's single digit then it would be 0xmonth, if it's double digits then it would be 0 + xx month which are returned.

See snippet for example:

var d = '0' + (new Date().getMonth() + 1).toString().slice(-2) + '-' + new Date().getFullYear().toString();
document.getElementById("demo").innerHTML = d;
<p id="demo"></p>

Alternatively if you don't want a trailing 0 on double digit months (Oct, Nov, Dec) you could do a little checking based on month digit length: (new Date().getMonth() + 1).length > 1 ? new Date().getMonth() + 1 : '0' + (new Date().getMonth() + 1)

var month = (new Date().getMonth() + 1).length > 1 ? new Date().getMonth() + 1 : '0' + (new Date().getMonth() + 1);
var date = (month).toString().slice(-2) + '-' + new Date().getFullYear().toString();
document.getElementById("demo").innerHTML = date;
<p id="demo"></p>
Mukyuu
  • 6,436
  • 8
  • 40
  • 59
4

You can use the built-in date pipe:

{{date | date:'MM-dd'}}

and pass your own format.
Update
Try something like for a JS-only solution:

m = new Date().getMonth().toString() + 1;
y = new Date().getFullYear().toString();
return m + '-' + y;
ItFreak
  • 2,299
  • 5
  • 21
  • 45
4

Alternatively, you can import DatePipe, or FormatDate into your component.ts.

1) DatePipe:

import { DatePipe } from '@angular/common';

export class SampleComponent implements OnInit {   

constructor(private datePipe: DatePipe) {}

  transformDate(date) {
    return this.datePipe.transform(date, 'MM-yyyy');
  }
}

Do not forget to add DatePipe to your providers in your module.

providers: [DatePipe]

2) formatDate:

import { formatDate } from '@angular/common';

export class SampleComponent implements OnInit {

  constructor(@Inject(LOCALE_ID) private locale: string) {}

  transformDate(date) {
    return formatDate(date, 'MM-yyyy', this.locale);
  }
}
wentjun
  • 40,384
  • 10
  • 95
  • 107