Home > Enterprise >  How to convert mmddyyyy date format to mm/dd/yyyy in nodejs [duplicate]
How to convert mmddyyyy date format to mm/dd/yyyy in nodejs [duplicate]

Time:10-05

I have a dateformat

date="11032020"(mmddyyyy)

output expected:11/03/2020

I want to convert this to mm/dd/yyyy in nodejs.I am a begineer to nodejs. I saw many posts where they convert from mm-dd-yyyy to different format or related.I tried converting to ISO but that did not work out.

I am not able to convert mmddyyyy to different format.

Could you please help me out in this.

CodePudding user response:

If you're certain that the input is MMDDYYYY and don't need to validate it, you could use a regular expression:

let format_date = input => input.replace(/(\d{2})(\d{2})(\d{4})/, "$1/$2/$3")
format_date("11032020") // "11/03/2020"

CodePudding user response:

With moment.js you can do:

const date = "11032020"
const res = moment(date, "MMDDYYYY").format('MM/DD/YYYY')

console.log(res)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8 M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

  • Related