在Chrome 5中使用date.format [英] date.format in Chrome 5

查看:113
本文介绍了在Chrome 5中使用date.format的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果我使用控制台并键入:

  d = new Date(Date.parse(2010-05-28))
d.format(yyyy-MM- DD);

d._toFormattedString();
Fri 2010年5月28日00:00:00 GMT + 0200(W. Europe Daylight Time)

任何人都知道为什么我会得到一个空字符串?它只在Chrome中,没有在Chrome 4中发生,并且在IE8 / Firefox3.5中没有发生。

解决方案

中没有格式 code> Date object。



您是否从插件获取该函数?

如果是这样,那么我建议你阅读API寻找可能的参数。

我不知道这是否有帮助,但我尝试过的所有插件都需要两个y 以获得四位数的字符串。

  d = new Date(Date.parse(2010-05-28)) )
d.format(yy-MM-dd);

编辑:其他一些插件使用大写字母Y,如 d。格式(YMD);



EDIT2 :看起来您需要将日期格式设置为 YYYY-MM-DD。
您可以在代码中添加这个原型函数:
$ b $ pre $ Date.prototype.toMyPreferredFormat = function(){
var dd = this.getDate();
if(dd <10)dd ='0'+ dd;
var mm = this.getMonth()+ 1;
if(mm <10)mm ='0'+ mm;
var yyyy = this.getFullYear();
return String(yyyy + - + mm + - + dd);
}

并使用它:

  d = new Date(Date.parse(2010-05-28))
d.toMyPreferredFormat();

原型函数的来源


I'm experiencing something really strange with my javascript in chrome with Date().format.

If I use the console and type:

d = new Date(Date.parse("2010-05-28"))
d.format("yyyy-MM-dd");
""
d._toFormattedString();
"Fri May 28 2010 00:00:00 GMT+0200 (W. Europe Daylight Time)"  

Anyone got any clue why I get an empty string? And it's only in Chrome, didn't happen in Chrome 4, and it doesn't happen in IE8/Firefox3.5

解决方案

There is no format function in the Date object.

Are you getting that function from a plugin?
If so then I suggest you read the APIs looking for the possible parameters.

I don't know if this might help, but all the plugins I tried required two "y" to get the four digit year string.

d = new Date(Date.parse("2010-05-28"))
d.format("yy-MM-dd");

EDIT: Some other plugin uses the uppercase "Y", like d.format("Y-m-d");

EDIT2: It seems like you need to just format your date to "yyyy-MM-dd". You can add this prototype function in your code:

Date.prototype.toMyPreferredFormat = function(){
  var dd=this.getDate();
  if(dd<10)dd='0'+dd;
  var mm=this.getMonth()+1;
  if(mm<10)mm='0'+mm;
  var yyyy=this.getFullYear();
  return String(yyyy+"-"+mm+"-"+dd);
}

And use it like:

d = new Date(Date.parse("2010-05-28"))
d.toMyPreferredFormat();

Source of the prototype function

这篇关于在Chrome 5中使用date.format的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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