0

This script works perfectly in Chrome, but I get undefined in Safari. Do you know how to solve it?

var dateString = "2013-07-31 12:00:00";

var daysOfWeek = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
var whichDay = daysOfWeek[new Date(dateString).getDay()];

alert(whichDay);

JSfiddle: http://jsfiddle.net/utn3yyzc/

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Kristoffer
  • 1,984
  • 3
  • 14
  • 29
  • Maybe this helps you: http://stackoverflow.com/questions/3085937/safari-js-cannot-parse-yyyy-mm-dd-date-format . Safari probably can't convert dateString to a valid Date object, then you get undefined. – juliano.net Apr 13 '16 at 19:53
  • `console.log(new Date("2013-07-31 12:00:00"));` returns "Invalid Date" – Joseph Marikle Apr 13 '16 at 19:55
  • 1
    When a JS program doesn't work as expected, the first thing you should do is check the browser console for error messages. That would have helped you understand the problem. – Barmar Apr 13 '16 at 19:55
  • 1
    If you need to parse a non-standard date format, you should look into using moment.js. – Barmar Apr 13 '16 at 19:56
  • Here's one with a workable date format: http://jsfiddle.net/gLr6z8a4/ – Joseph Marikle Apr 13 '16 at 19:58

2 Answers2

2

Let's check out what MDN has to say about sending a date-string into a Date constructor.

MDN : Global Objects : Date


Syntax

new Date(dateString);

Parameters

dateString

String value representing a date. The string should be in a format recognized by the Date.parse() method (IETF-compliant RFC 2822 timestamps and also a version of ISO8601).

Note: parsing of date strings with the Date constructor (and Date.parse, they are equivalent) is strongly discouraged due to browser differences and inconsistencies.

We see that this method of parsing is unreliable and "strongly discouraged", so we must implement our own parsing or use a library that can do this for us.


Manual Way

Simply parse the date from the given string.

function parseDate(datestring) {
  var d = datestring.split(/\D+/g).map(function(v) { return parseInt(v, 10); });
  return new Date(d[0], d[1] - 1, d[2], d[3], d[4], d[5]);
}

var dateString = "2013-07-31 12:00:00";
var daysOfWeek = [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ];
var whichDay = daysOfWeek[parseDate(dateString).getDay()];

alert(whichDay);

Library (MomentJS) Solution

var whichDay = moment('2013-07-31 12:00:00', 'YYYY-MM-DD HH:mm:ss').format('dddd');

alert(whichDay);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.12.0/moment.min.js"></script>

Update

Here is a modern version that used i18n:

const
  parseDate = (datestring) => 
    (([year, month, date, hour, minute, second]) =>
      new Date(year, month - 1, date, hour, minute, second))
    (datestring.split(/\D+/g).map(v => parseInt(v, 10))),
  dateString = '2013-07-31 12:00:00',
  whichDay = parseDate(dateString)
    .toLocaleString('en-US', { weekday: 'long' });

console.log(whichDay);
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
0

new Date(dateString) returns an invalid date.

I'd suggest parsing your date string in a more explicit way (Safari seems to not support this format).

For instance:

var dateString = "2013-07-31 12:00:00";

var split = dateString.split(' ');
var dateSplit = split[0].split('-');
var hourSplit = split[1].split(':');

var d = new Date(dateSplit[0], dateSplit[1] - 1, dateSplit[2],     hourSplit[0], hourSplit[1], hourSplit[2]);