最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Convert date in YYYYMMDD format in MomentJS - Stack Overflow

programmeradmin13浏览0评论

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
  • What's the output of the code you tried? And try this format: MM/DD/YYYY – Casper Commented Dec 2, 2014 at 8:40
  • I have date "12/02/2014" in this format I want to convert in "2014/12/02" – FrontEnd Expert Commented Dec 2, 2014 at 8:41
  • Use new Date("2010/12/01") instead of new Date(2010/12/01). But RobGs answer is better. – dusky Commented Dec 2, 2014 at 8:43
Add a comment  | 

3 Answers 3

Reset to default 9

You 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');
发布评论

评论列表(0)

  1. 暂无评论