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

javascript - date-fns convert string date format 'MMM-DD-YYYY' into Date - Stack Overflow

programmeradmin5浏览0评论

How can I convert using date-fns the following string into Date?

Jul-30-2021 (format MMM-DD-YYYY).

Using momentjs I can convert it using:

moment('Jul-30-2021', 'MMM-DD-YYYY')

How can I convert using date-fns the following string into Date?

Jul-30-2021 (format MMM-DD-YYYY).

Using momentjs I can convert it using:

moment('Jul-30-2021', 'MMM-DD-YYYY')
Share Improve this question asked Jul 30, 2021 at 16:10 Stefano BorzìStefano Borzì 2,5165 gold badges24 silver badges41 bronze badges 3
  • 2 What is the input & expected output? – Rajendran Nadar Commented Jul 30, 2021 at 16:21
  • input: Jul-30-2021 output: Date object with the date Jul-30-2021 and 00:00:00 time or current time – Stefano Borzì Commented Aug 3, 2021 at 12:30
  • Try this new Date('Jul-30-2021').toISOString() after that you can use format from Date-fns to convert to any format. – Rajendran Nadar Commented Aug 3, 2021 at 12:37
Add a ment  | 

2 Answers 2

Reset to default 6

<script type="module">
  import { format } from 'https://esm.run/date-fns'

  const date = new Date('Jul-30-2021');

  console.log(date);

  console.log(format(date, 'MMM-dd-yyyy, mm:HH:ss'));
</script>

Now you can apply the date-fns format(date, 'MMM-dd-yyyy')

this way :

const setDateMDY = dteSTR => 
  {
  let [m,d,y] = dteSTR.split('-')
  return new Date(`${m} ${d}, ${y} 00:00:00`)
  }

let  date1 = setDateMDY('Jul-30-2021')

console.log( date1.toLocaleString() )

发布评论

评论列表(0)

  1. 暂无评论