Reputation: 415
I want to convert date from the format 2020-02-26
to Wed 02/26
? I have tried the below code but this is not what I wanted
var weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var a = new Date(order_date);
alert(weekday[a.getDay()]);
Upvotes: 1
Views: 144
Reputation: 8479
Use Moment.js if you have plenty of work to do with dates and formatting them.
let now = moment('2020-02-26').format('ddd MM/DD');
console.log(now)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
Format Dates
moment().format('MMMM Do YYYY, h:mm:ss a'); // February 25th 2020, 4:44:28 pm
moment().format('dddd'); // Tuesday
moment().format("MMM Do YY"); // Feb 25th 20
moment().format('YYYY [escaped] YYYY'); // 2020 escaped 2020
moment().format(); // 2020-02-25T16:44:28+08:00
Relative Time
moment("20111031", "YYYYMMDD").fromNow(); // 8 years ago
moment("20120620", "YYYYMMDD").fromNow(); // 8 years ago
moment().startOf('day').fromNow(); // 17 hours ago
moment().endOf('day').fromNow(); // in 7 hours
moment().startOf('hour').fromNow(); // an hour ago
Multiple Locale Support
moment.locale(); // en
moment().format('LT'); // 4:47 PM
moment().format('LTS'); // 4:47:14 PM
moment().format('L'); // 02/25/2020
moment().format('l'); // 2/25/2020
moment().format('LL'); // February 25, 2020
moment().format('ll'); // Feb 25, 2020
moment().format('LLL'); // February 25, 2020 4:47 PM
moment().format('lll'); // Feb 25, 2020 4:47 PM
moment().format('LLLL'); // Tuesday, February 25, 2020 4:47 PM
moment().format('llll'); // Tue, Feb 25, 2020 4:47 PM
moment.js has come to an end. In this official doc, it is said that:
We now generally consider Moment to be a legacy project in maintenance mode. It is not dead, but it is indeed done.
Use Intl.DateTimeFormat instead as in @Masklinn's answer
Upvotes: 1
Reputation: 42502
As an alternative to using moment, modern browsers have the Intl
namespace which amongst other things has a skeleton-based DateTimeFormat.
"Skeleton-based" means instead of passing a specific format string, you specify which fields you want to show (and how) and it'll try to find the shortest format which satisfies the constraints in the locale you've specified:
>> Intl.DateTimeFormat(undefined, {weekday: 'short', month: 'numeric', day: 'numeric'}).format(new Date)
< "Tue, 25/02"
>> Intl.DateTimeFormat('de-DE', {weekday: 'short', month: 'numeric', day: 'numeric'}).format(new Date)
< "Di., 25.2."
>> Intl.DateTimeFormat('ja-JP', {weekday: 'short', month: 'numeric', day: 'numeric'}).format(new Date)
< "2/25(火)"
>> Intl.DateTimeFormat('fr-CA', {weekday: 'short', month: 'numeric', day: 'numeric'}).format(new Date)
< "mar. 2-25"
Upvotes: 1
Reputation: 337701
To create that date format you'll need to retrieve the relevant parts of the date and piece them together in a string manually, like this:
const weekday = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
let d = new Date(); // order_date
let day = weekday[d.getDay()];
let month = ('00' + (d.getMonth() + 1)).slice(-2);
let date = ('00' + d.getDate()).slice(-2);
let output = `${day} ${month}/${date}`;
console.log(output);
Upvotes: 2