-2

I am using ion2 calendar for creating a multidatepicker. Here I am using onSelect event emitter.

onSelect($event) {

 console.log("onSelect event called");
 console.log($event);  

};

Here when I print '$event' the console output looks like this:

How to extract only date from this moment object in the format 'DD-MMMM-YYYY'?

screenshot

Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
Chaitra R
  • 9
  • 7

2 Answers2

0

Below one liner will give you desired output:

let formattedDate = moment($event[0].Moment._d).format('DD-MMMM-YYYY');

So your function will become

onSelect($event) {
    let formattedDate = moment($event[0].Moment._d).format('DD-MMMM-YYYY');
    console.log("onSelect event called");
    console.log($event);  
};

Note : As I can not debug your code so please check first that if $event[0].Moment._d is giving you highlighted date (_d: Wed May 16 ........) in your question.

Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
  • Hi VicJordan, Thanks for the response. DD-MMMM-YYYY format is for displaying full name of a month. For ex : 28 August 2014 – Chaitra R May 02 '18 at 05:29
  • @ChaitraR just updated my answer. It will work and print full month name. Check it out. – Vikasdeep Singh May 02 '18 at 05:46
  • I am getting this error : TypeError: Cannot read property '_d' of undefined – Chaitra R May 02 '18 at 05:56
  • @ChaitraR updated answer. Check with `$event[0].Moment._d`. It should work. – Vikasdeep Singh May 02 '18 at 06:13
  • Now I got this error : TypeError: Cannot read property 'Moment' of undefined – Chaitra R May 02 '18 at 06:16
  • Let me try $event[0].Moment[0]._d – Chaitra R May 02 '18 at 06:17
  • @ChaitraR can you share `$event` value in JSON format? – Vikasdeep Singh May 02 '18 at 06:18
  • It looks as above. In question itself I have given the json format. – Chaitra R May 02 '18 at 06:21
  • @ChaitraR that is screenshot. I can not debug the screenshot data. Give the data so that I can run it locally and solve your problem. – Vikasdeep Singh May 02 '18 at 06:22
  • @ChaitraR if you dont know how to copy data then check this question: https://stackoverflow.com/a/25140576/631803 – Vikasdeep Singh May 02 '18 at 06:23
  • I am getting only this line: "2018-05-09T18:30:00.000Z" – Chaitra R May 02 '18 at 06:57
  • Moment {_isAMomentObject: true, _i: 1525890600000, _isUTC: false, _pf: {…}, _locale: Locale, …} _d : Thu May 10 2018 00:00:00 GMT+0530 (India Standard Time) {} _i : 1525890600000 _isAMomentObject : true _isUTC : false _isValid : true _locale : Locale {_calendar: {…}, _longDateFormat: {…}, _invalidDate: "Invalid date", ordinal: ƒ, _dayOfMonthOrdinalParse: /\d{1,2}(th|st|nd|rd)/, …} _pf : {empty: false, unusedTokens: Array(0), unusedInput: Array(0), overflow: -2, charsLeftOver: 0, …} __proto__ : Object – Chaitra R May 02 '18 at 06:59
  • Not sure how I am getting it. When I tried to use copy(temp1) I got that. So I did console.log(temp1) copied the object and pasted here. – Chaitra R May 02 '18 at 07:08
  • @ChaitraR paste your JSON data here and share the link : http://myjson.com/ – Vikasdeep Singh May 02 '18 at 07:10
  • @ChaitraR tell me about this one line? How you are getting this value "2018-05-09T18:30:00.000Z"? – Vikasdeep Singh May 02 '18 at 07:10
  • Moment {_isAMomentObject: true, _i: 1525890600000, _isUTC: false, _pf: {…}, _locale: Locale, …} _d : Thu May 10 2018 00:00:00 GMT+0530 (India Standard Time) {} _i : 1525890600000 _isAMomentObject : true _isUTC : false _isValid : true _locale : Locale {_calendar: {…}, _longDateFormat: {…}, _invalidDate: "Invalid date", ordinal: ƒ, _dayOfMonthOrdinalParse: /\d{1,2}(th|st|nd|rd)/, …} _pf : {empty: false, unusedTokens: Array(0), unusedInput: Array(0), overflow: -2, charsLeftOver: 0, …} __proto__ : Object – Chaitra R May 02 '18 at 07:24
  • I got the solution. Now I am able to print the extracted date in the console. onSelect($event) { console.log("onSelect event called"); console.log(JSON.stringify($event.format('DD-MMMM-YYYY'))); }; – Chaitra R May 02 '18 at 08:57
0

In your case just use moment($event[0]).format('DD-MMMM-YYYY')

console.log(moment().format('DD-MMMM-YYYY'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.js"></script>
Durga
  • 15,263
  • 2
  • 28
  • 52