最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Alternative to toLocaleString() for chrome browser - Stack Overflow

programmeradmin5浏览0评论
 function tolocal(str)
 {
 var date, split, dSplit, tSplit, d, raw;
date = '';
split = str.split(' ');
if (split.length === 2) {
    dSplit = split[0].split('-');
    tSplit = split[1].split(':');
}
raw = d.toLocaleString().split(' GMT')[0];

 return raw.substring(raw.indexOf(", ")+2, raw.lastIndexOf(':')) + " " + raw.substring(raw.length-2,raw.length)
 }

The above code, works well in ie browser where I get the output in the following format.

November 13,2012 10:15 AM

But I am not able to achieve the same in the chrome browser. Is there any other function which will help me achieve the same output? date.toUTCString() provides the same result but I am not sure how different it is to toLocaleString() in terms of functionality.

Thanks in advance.

 function tolocal(str)
 {
 var date, split, dSplit, tSplit, d, raw;
date = '';
split = str.split(' ');
if (split.length === 2) {
    dSplit = split[0].split('-');
    tSplit = split[1].split(':');
}
raw = d.toLocaleString().split(' GMT')[0];

 return raw.substring(raw.indexOf(", ")+2, raw.lastIndexOf(':')) + " " + raw.substring(raw.length-2,raw.length)
 }

The above code, works well in ie browser where I get the output in the following format.

November 13,2012 10:15 AM

But I am not able to achieve the same in the chrome browser. Is there any other function which will help me achieve the same output? date.toUTCString() provides the same result but I am not sure how different it is to toLocaleString() in terms of functionality.

Thanks in advance.

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Oct 15, 2013 at 10:47 JinxedJinxed 7367 silver badges28 bronze badges 5
  • They're pletely different. What do you need, martes, 15 de octubre de 2013 12:53:46 or Tue, 15 Oct 2013 10:53:46 GMT? – Álvaro González Commented Oct 15, 2013 at 10:54
  • I need it in October 15,2013 10:53 AM as the final output. I can make few string manipulations to achieve this. But toLocaleString() in chrome gives me 15/10/2013 10:53:00 AM – Jinxed Commented Oct 15, 2013 at 11:01
  • 1 So you don't need locale features at all (neither language nor time zone): you want to convert to UTC and display in English with custom format. Sorry, I was confused by the alternative to toLocaleString() part. – Álvaro González Commented Oct 15, 2013 at 11:19
  • So you're looking for it to be represented as UTC time in the custom format given? Month DD, YYYY HH:MM AM – Qantas 94 Heavy Commented Oct 15, 2013 at 12:22
  • I want it in Month DD, YYYY HH:MM AM format – Jinxed Commented Oct 15, 2013 at 12:59
Add a ment  | 

3 Answers 3

Reset to default 3

Just do it manually:

// Where "date" is a Date object
function dateFormatUTC(date) {
  var months = [
    'January', 'February', 'March', 'April', 'May', 'June',
    'July', 'August', 'September', 'October', 'November', 'December'
  ];

  var hours = date.getUTCHours();
  if (hours < 10) hours = '0' + hours;

  var minutes = date.getUTCMinutes();
  if (hours < 10) hours = '0' + hours;

  var monthName = months[date.getUTCMonth()];
  var timeOfDay = hours < 12 ? 'AM' : 'PM';

  return monthName + ' ' + date.getUTCDate() + ', ' +
         date.getUTCFullYear() + ' ' + hours + ':' + minutes + timeOfDay;
}

maybe you can use a thirdparty library to do stuff like that: moment.js is a good one. Example:

moment(d).format('MMMM Do, YYYY h:mms a');

you can try using options like below:

  var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
          // request a weekday along with a long date
   var options = {weekday: "long", year: "numeric", month: "long", day: "numeric"};
     // an application may want to use UTC and make that visible
    options.timeZone = "UTC";
    options.timeZoneName = "short";
    alert(date.toLocaleString("en-US", options));

Please find the reference @

https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString

发布评论

评论列表(0)

  1. 暂无评论