Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Remove Day Name from Date

i wanted to ask if someone knows how to remove the Day Name from the following example,the alert returns Sat Feb 29 2020, im not using Moment.js only Jquery because i only need to be able to handle the date in the format that is written below as code.

var mydate = new Date('29 Feb 2020');
alert(mydate.toDateString());

Thank you for reading this question and hope i make clear what my problem is

like image 632
Magthuzad Avatar asked Jan 22 '18 14:01

Magthuzad


People also ask

How to remove day from date in JavaScript?

To subtract days to a JavaScript Date object, use the setDate() method. Under that, get the current days and subtract days. JavaScript date setDate() method sets the day of the month for a specified date according to local time.

How to remove time part from date in JavaScript?

Use the toDateString() method to remove the time from a date, e.g. new Date(date. toDateString()) . The method returns only the date portion of a Date object, so passing the result to the Date() constructor would remove the time from the date.

How do you subtract days in JavaScript?

setDate(date. getDate() - numOfDays); return date; } // 👇️ Subtract 1 day from the current date const result = subtractDays(1); // 👇️ Subtract 2 days from another date // 👇️ Tue Mar 22 2022 console.


1 Answers

The Date#toDateString method would result always returns in that particular format.

So either you need to generate using other methods available or you can remove using several ways,


1. Using String#split, Array#slice and Array#join

var mydate = new Date('29 Feb 2020');
// split  based on whitespace, then get except the first element
// and then join again
alert(mydate.toDateString().split(' ').slice(1).join(' '));


2. Using String#replace

var mydate = new Date('29 Feb 2020');
// replace first nonspace combination along with whitespace
alert(mydate.toDateString().replace(/^\S+\s/,''));


3. Using String#indexOf and String#substr
var mydate = new Date('29 Feb 2020');
// get index of first whitespace
var str = mydate.toDateString();
// get substring
alert(str.substr(str.indexOf(' ') + 1));
like image 195
Pranav C Balan Avatar answered Sep 18 '22 03:09

Pranav C Balan