7

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

Magthuzad
  • 140
  • 1
  • 10
  • 1
    just don't use `toDateString()` and format your date your own way because that function always return that format – CME64 Jan 22 '18 at 14:48
  • I had read about that but i dont really understand how to properly format dates in javascript, so i though that there could be a way to just cut out the day name, anyway thank you for answering – Magthuzad Jan 22 '18 at 14:55
  • Possible duplicate of [How to format a JavaScript date](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – Heretic Monkey Jan 22 '18 at 15:13

4 Answers4

18

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));
Pranav C Balan
  • 106,305
  • 21
  • 136
  • 157
2

If you've got a Date object instance and only want some parts of it I'd go with the Date object API:

mydate.getDate() + ' ' + mydate.toLocaleString('en-us', { month: "short" }) + ' ' + mydate.getFullYear()

Just keep in mind the functions are local time based (there are UTC variants, e.g. getUTCDate()), also to prevent some confusion getMonth() is zero-based. Working with dates in JavaScript is where the real fun begins ;)

The toLocaleString function is relatively new though (IE11+), check other possibilities if you need to support older browsers.

Tomas Varga
  • 1,254
  • 13
  • 22
  • So thats how you manually create a Date with the Javascript API? Thank you! – Magthuzad Jan 22 '18 at 15:03
  • Not a problem, it's always good to know you can format a date from its parts instead of fiddling with outputs of predefined-format functions (`toDateString`, `toISOString`) or using an external library. – Tomas Varga Jan 22 '18 at 15:16
0

Easiest way is to replace all alpha characters from a string. That way you will not make mistake once day name is in a different position.

var withoutDay = '29 Feb 2020'.replace(/[a-zA-Z]{0,1}/g,'').replace('  ', ' ');
alert(withoutDay);

Code replace(/[a-zA-Z]{0,1}/g,'') will replace all alpha characters from string and replace(' ', ' '); will remove double spaces.

I hope this helps.

Senad Meškin
  • 12,841
  • 4
  • 37
  • 53
0

Proper way is to use DateTimeFormat. You can play around by manipulating the format object inside DateTimeFormat.

let myDate = new Date('29 Feb 2020');
let formattedDate = new Intl.DateTimeFormat("en-US", {
  year: "numeric",
  month: "short",
  day: "2-digit",
}).format(myDate);

alert(formattedDate)
MSM
  • 320
  • 3
  • 15