I have a requirement of generating dates in this format 'Thru: 12/20'
(like credit/debit card expiry date format). What is the best way of generating date in this format?
new Date().toJSON().slice(0,7).split('-').reverse().join('/')
I got the date in mm/yyyy format but couldn't get the desired result
CodePudding user response:
You can use Intl.DateTimeFormat
to generate date in day name and dd/MM format
const options = { weekday: 'short', month: 'numeric', day: 'numeric' };
const result = new Intl.DateTimeFormat('en-GB', options).format(new Date());
console.log(result);
CodePudding user response:
With Moment you can simply do:
const date = moment().format("ddd DD/MM");
console.log("Valid until : " date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>