Scott
Scott

Reputation: 21890

Jquery date string not functional in Safari/Firefox, okay in Chrome

I can't seem to figure out why this following does not display the date inside a span when using Safari 6.0.4 or Firefox 20 on the Mac (haven't tested Windows yet).

HTML:

<h1>Some Text <span id="dt"></span></h1>

jQuery:

jQuery(document).ready(function() {

var d = new Date();
var month = d.getMonth()+1;
var day = d.getDate();
var TodaysDate = month + '/' + day + '/' +  d.getFullYear() + '-';
var tDate = new Date(TodaysDate).toUTCString();
var dt = tDate.slice(0, - 12);
//The Date string
$('#dt').text(dt);

});

I've tried replacing $('#dt').text(dt); with $('#dt').html(dt); and there was no change.

This fills the #dt span with the date. It works fine in Chrome, but displays nothing in Firefox and Safari.

jsFiddle here

Any insight wold be appreciate.

Upvotes: 1

Views: 252

Answers (2)

John Kroetch
John Kroetch

Reputation: 1143

Already answered, but if you're open to the idea of using small helper libraries, I really like this one: http://momentjs.com/

It's a very nice wrapper. To get the current date/time just do this:

var now = moment();

To format it to a UTC string, just do this:

now.utc().format(<your params>)

Upvotes: 1

j08691
j08691

Reputation: 208002

Get rid of the - in:

var TodaysDate = month + '/' + day + '/' +  d.getFullYear() + '-';

That generates 5/6/2013- which apparently only Chrome tolerates.

Change it to:

var TodaysDate = month + '/' + day + '/' + d.getFullYear();

jsFiddle example

Upvotes: 4

Related Questions