I am using MomentJS for converting the date format.
I have a date in this format 2010/12/01 and I want to convert it to 01/12/2010 using MomentJS only.
I have another date in this format "12/02/2014" and I want to convert in "2014/12/02"
<!--begin snippet: js hide: false console: true babel: false-->
<!--language: lang-js-->
var date = moment(new Date(2010/12/01)).format("MMM Do h/mm");
console.log(date);
<!--language: lang-html-->
<script src=".js/2.14.1/moment.min.js"></script>
<!--end snippet-->
How do I convert them using MomentJS
I am using MomentJS for converting the date format.
I have a date in this format 2010/12/01 and I want to convert it to 01/12/2010 using MomentJS only.
I have another date in this format "12/02/2014" and I want to convert in "2014/12/02"
<!--begin snippet: js hide: false console: true babel: false-->
<!--language: lang-js-->
var date = moment(new Date(2010/12/01)).format("MMM Do h/mm");
console.log(date);
<!--language: lang-html-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>
<!--end snippet-->
How do I convert them using MomentJS
Share Improve this question edited Oct 20, 2016 at 7:59 Diamond 7,48325 silver badges38 bronze badges asked Dec 2, 2014 at 8:34 FrontEnd ExpertFrontEnd Expert 5,80310 gold badges60 silver badges99 bronze badges 3 |3 Answers
Reset to default 9You can try this:
moment('2010/12/01', 'YYYY/MM/DD').format('DD/MM/YYYY')
You don't need a Date library at all:
'2010/12/01'.split('/').reverse().join('/')
sudoCode
moment('date-to-be-converted','format-of-date-to-be-converted').format('desired-format');
moment('12/02/2014','MM/DD/YYYY').format('YYYY/MM/DD');
new Date("2010/12/01")
instead ofnew Date(2010/12/01)
. But RobGs answer is better. – dusky Commented Dec 2, 2014 at 8:43