开发者

Best way to remove 'EDT' from a date returned via javascript with toLocaleString()

I'm rel开发者_如何学Goatively new to javascript, so this may be a really simple question. Is there an easy way to stop 'EDT' from printing after a date returned with toLocaleString? Thanks!


There is no way to be certain what toLocaleString will return; you certainly couldn't guarantee EDT would show up on every machine that runs it, let alone any indication of timezone.

From Mozilla's developer network:

The toLocaleString method relies on the underlying operating system in formatting dates. It converts the date to a string using the formatting convention of the operating system where the script is running. For example, in the United States, the month appears before the date (04/15/98), whereas in Germany the date appears before the month (15.04.98). If the operating system is not year-2000 compliant and does not use the full year for years before 1900 or over 2000, toLocaleString returns a string that is not year-2000 compliant. toLocaleString behaves similarly to toString when converting a year that the operating system does not properly format.

One possible workaround would be to construct a custom date string using toLocaleDateString and toLocaleTimeString.

// Something to this effect:
var d = new Date();
console.log(d.toLocaleDateString() + " " + d.toLocaleTimeString());

This generally wouldn't include the time zone in its output, but even this isn't perfect as you can't know what the exact format of the output would be.

Thus, the best solution would be to use a custom date-formatting function:

// Add leading-zeros to numbers less than 10[000...]
function padZ(num, n) {
    n = n || 1; // Default assume 10^1
    return num < Math.pow(10, n) ? "0" + num : num;
}

function formattedDate(d) {
    var day = d.getDate();
    var month = d.getMonth() + 1; // Note the `+ 1` -- months start at zero.
    var year = d.getFullYear();
    var hour = d.getHours();
    var min = d.getMinutes();
    var sec = d.getSeconds();
    return month+"/"+day+"/"+year+" "+hour+":"+padZ(min)+":"+padZ(sec);
}

For an in-depth look at the available Date methods, check out Date on the MDN.


As far as I can tell, no browser returns 'EDT' from toLocaleString, on windows anyway, and only Chrome returns the timezone at all.

Other platforms may assign the string differently.

My bigger beef is that Chrome uses a 24 hour clock for local time.

// testing new Date().toLocaleString() (windows 7)

  • Safari 5.0>> Tuesday, June 14, 2011 15:13:43
  • Chrome 9.0.597.98>> Tue Jun 14 2011 15:15:01 GMT-0400 (Eastern Daylight Time)
  • Opera 11.01>> 6/14/2011 3:15:37 PM
  • Firefox 4.0.1>> Tuesday, June 14, 2011 3:16:33 PM
  • MSIE 8.0>> Tuesday, June 14, 2011 3:16:06 PM
  • MSIE 9.0>> Tuesday, June 14, 2011 3:17:09 PM

They all return the hours:minutes:seconds in a group, so to exclude anything after the time you could:

var d=new Date().toLocaleString();
var s= d.toLocaleString().match(/^[^:]+(:\d\d){2} *(am|pm)\b/i)[0];

returned value: (Chrome)
Tue Jun 14 2011 15:26:11:11

Another way is to concat the locale day and time strings, which, surprisingly, does not return the timezone on chrome- but your milage may vary.

var D=new Date();
D.toLocaleDateString()+' '+D.toLocaleTimeString()

returns Tuesday, June 14, 2011 15:44:35 in Chrome


var dateWithoutTimeZone = function() {
  return new Date().toLocaleString().replace(/\s*\(?EDT\)?/, '');
};
dateWithoutTimeZone(); // => "Tue Jun 14 2011 2:58:04 GMT-0400"


Is the time zone always at the end of the string? (The time zone doesn't appear at all when I try.)

If it is, you can use the slice method to remove the last four characters of the string (space + 3-character time zone).

document.write(mydate.toLocaleString().slice(0,-4));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜