toLocaleDateString Javascript日期格式问题 [英] toLocaleDateString Javascript date format issues

查看:1325
本文介绍了toLocaleDateString Javascript日期格式问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,我想将日期格式化为短日期格式,即:

I've got a script and i want to format a date out to short date format ie:

7/3/2013或7/3/13第一个日期格式像在Chrome中渲染,但每个其他浏览器它不显示日期月份名称和年份。

7/3/2013 or 7/3/13 the first date format renders like that in Chrome but every other browser it does not - it displays the date month name and the year.

function dateFormatter(date) {
  return date.toLocaleDateString();
}

比特混淆了为什么会发生这种情况。是因为浏览器不支持tolocalDateString();

Bit confused as to why this is happening. Is it because that browser doesnt support tolocalDateString();

我需要创建一个自定义日期字符串才能正常工作?

Would i need to build a custom date string in order for it to work?

对不起,如果它有点模糊 - 我看过W3C网站,但不信任该网站有时。

Sorry if its a little vague - I've had a look on W3C website but dont trust that site at times.

推荐答案

function dateFormatter(date){
    if(Date.parse('2/6/2009')=== 1233896400000){
        return [date.getMonth()+1, date.getDate(), date.getFullYear()].join('/');
    }
    return [date.getDate(), date.getMonth()+1, date.getFullYear()].join('/');
}

这篇关于toLocaleDateString Javascript日期格式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆