2

Due to the language barrier I am unable to find an answer to a problem I'm having. Using 2 arrays in JavaScript I need to translate a date.

var months =['january','february','march','april','may','june','july','august','september','october'.'november','december'];
var weekday = ['sunday','monday','tuesday','wednesday','thursday','friday','saturday'];

Using this I am supposed to turn for example:

Mon Oct 28 2018 14:41:20 GMT+0100
into
Monday 28 October 2018

My apologies for this likely being a repost of a question that has been answered previously, but as I previously mentioned I was unable to find it.

str
  • 42,689
  • 17
  • 109
  • 127
  • 2
    If you're open to using libraries, try [MomentJs](https://momentjs.com/) and don't reinvent the wheel: `moment(myDate).format('dddd DD MMMM YYYY');` – UncleDave Jan 12 '18 at 13:17
  • You might be able to test the input strings with regular expressions. Regex may help. – Dream_Cap Jan 12 '18 at 13:18

3 Answers3

3

Use moment.js

moment(new Date('Mon Oct 28 2018 14:41:20 GMT+0100')).format('dddd DD MMMM YYYY')
Jerry Chen
  • 715
  • 7
  • 21
  • "Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info." – str Jan 12 '18 at 13:30
  • ```moment(new Date('Mon Oct 28 2018 14:41:20 GMT+0100'))..format('dddd DD MMMM YYYY')``` – Jerry Chen Jan 12 '18 at 13:34
  • 1
    "[...] Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged [...]" – str Jan 12 '18 at 13:40
0

You don't need external libraries in order to achieve your requirement.

You just need to use some Date functions.

var months = ['january','february','march','april','may','june','july','august','september','october','november','december'];

var weekday = ['sunday','monday','tuesday','wednesday','thursday','friday','saturday'];

var date=new Date();

console.log(getDate(date));

function getDate(date){
   return [
     weekday[date.getDay()],
     date.getDate(),
     months[date.getMonth()],
     date.getFullYear()
   ].join(' ');
}
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
  • 1
    Thank you, this was the type of answer I was looking for. However I cannot really use this, I started with jscript about a week ago so I don't know much yet. I need to display the answer on the page using document.write. Is there a way you/I could change this code so it could do that? Thank you. – kyl1an matth3w Jan 12 '18 at 13:40
  • Please don't recommend using the built-in parser, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results). Consider just reformatting the string *without* creating a Date object. – RobG Jan 14 '18 at 20:22
0

If you want to vanilla javascript solution:

A simple solution (if you can forgo the exact full month name):

var date = new Date("Mon Oct 28 2018 14:41:20 GMT+0100");

console.log(date.toDateString());

Or

Pass some options into toLocaleDateString:

var date = new Date("Mon Oct 28 2018 14:41:20 GMT+0100");

console.log(date.toLocaleDateString("en-US", {
  weekday: 'long',
  year: 'numeric',
  month: 'long',
  day: 'numeric'
}))

Check MDN Date documentation here.

n4m31ess_c0d3r
  • 3,028
  • 5
  • 26
  • 35