-1

I have the date in this format.I am using format() of monentjs. Currently mydate is in this format 2018-10-11T16:31:27+05:30

let newdate=let newdate=this.state.startDate.format();

I want to get this date in this format 2018-10-22. I tried using this

let formatfun=(date)=>{
      let x=moment(date,"moment.HTML5_FMT.DATE");
      return x;
    }

but could not reach there .

iamredpanda
  • 113
  • 3
  • 11

5 Answers5

0

const newdate = "2018-10-11T16:31:27+05:30"; // this.state.startDate.format();
const formatfun = date => {
    return date.substr(0, 10);
}
console.log(formatfun(newdate));

Would this help? Just taking the first 10 chars of your date string.

Or if it is of date type, look over at Format JavaScript Date to yyyy-mm-dd (possible duplicate).

Matthi
  • 1,073
  • 8
  • 19
0

Have you tried this?

let formatfun=(date)=>{
  let x=moment(date).format("YYYY-MM-DD");
  return x;
}
Michael H
  • 431
  • 4
  • 4
0

You can learn more about formatting with moment.js here.

let formatfun = (date) => {
   let x = moment(date).format('YYYY-MM-DD');
   return x;
}

let date = '2018-10-22T16:31:27+05:30';
console.log(date);
console.log(formatfun(date));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
Karan
  • 12,059
  • 3
  • 24
  • 40
0

try this way

moment("yourdate","dateformat").format("newformat");

let formatfun=(date)=>{
      let x=moment(date,"moment.HTML5_FMT.DATE").format('yyyy-dd-mm');
      return x;
}

read more here : https://momentjs.com/docs/#/parsing/string-format/

Saurabh Mistry
  • 12,833
  • 5
  • 50
  • 71
0

You can use moment(newdate).format("YYYY-MM-DD")

check following url for more reference :-

https://momentjs.com/docs/#/parsing/string-format/

Dheeman
  • 1
  • 2