简体   繁体   English

如何获取适用于所有浏览器的javascript日期分割代码?

[英]How to I get javascript date split code to work for all browsers?

I have a piece of code that works in Chrome and MSIE but failing in Opera and Firefox 我有一段可在Chrome和MSIE中运行的代码,但在Opera和Firefox中无法运行

var end = Browser.getValue(getElement("mydate"));
var parts = end.split('.');
var us_date = parts[1]+'/'+parts[0]+'/'+parts[2];


var someDate = new Date(us_date);
var numberOfDaysRemove = 1;
someDate.setDate(someDate.getDate() - numberOfDaysRemove);

var returndate = someDate.getDate() + '.' + (someDate.getMonth() + 1) + '.' + someDate.getFullYear() + " " + someDate.getHours() + ':' + someDate.getMinutes();

I know why it doesn't work in Opera and Firefox as I need to replace the 我知道为什么它在Opera和Firefox中不起作用,因为我需要替换

var parts = end.split('.');
var returndate = someDate.getDate() + '.' + (someDate.getMonth() + 1) + '.' + someDate.getFullYear() + " " + someDate.getHours() + ':' + someDate.getMinutes();

with

var parts = end.split('/');
var returndate = someDate.getDate() + '/' + (someDate.getMonth() + 1) + '/' + someDate.getFullYear() + " " + someDate.getHours() + ':' + someDate.getMinutes();

When I do this then it won't work in Chrome or MSIE. 当我这样做时,它将无法在Chrome或MSIE中运行。

Is there a way to get this code to work in all browsers? 有没有办法让此代码在所有浏览器中都能使用?

You should never parse strings using the Date constructor or Date.parse (which do the same thing) as it is almost entirely implementation dependent and unreliable. 永远不要使用Date构造函数或Date.parse(它们做同样的事情)来解析字符串,因为它几乎完全依赖于实现且不可靠。 Always manually parse strings, a library can help but if you only need to support one or two formats, a bespoke function is trivial. 总是手动解析字符串,库可以提供帮助,但是如果您只需要支持一种或两种格式,则定制功能是微不足道的。

Given that you have: 鉴于您有:

var end = Browser.getValue(getElement("mydate"));
var parts = end.split('.');
var us_date = parts[1]+'/'+parts[0]+'/'+parts[2];

I suspect end is something like "dd.mm.yyyy", so replace the last line with: 我怀疑结尾是“ dd.mm.yyyy”之类的东西,所以将最后一行替换为:

var someDate = new Date(parts[2], parts[1]-1, parts[0]);

which will work in every browser since javascript was invented. 自从javascript发明以来,它便可以在所有浏览器中使用。

Edit 编辑

Presuming that the date string is "dd.mm.yyyy", a simple function to parse it regardless of the separator is: 假定日期字符串为“ dd.mm.yyyy”,则不管分隔符如何,解析它的简单函数为:

 /* Parse date string in format d/m/y ** @param {string} s - date string ** @returns {Date} date object */ function parseDMY(s) { var b = s.split(/\\D/); return new Date(b[2], b[1]-1, b[0]); } document.write(parseDMY('23.01.2016')); 

This assumes that the values are a valid date and will allow any non–digit separator, so dmy, d/m/y and dmy are all acceptable. 假定值是有效日期,并且允许使用任何非数字分隔符,因此dmy,d / m / y和dmy都是可以接受的。

My suggestion is to use something like jquery to manipulate the DOM. 我的建议是使用类似jquery的东西来操作DOM。 Jquery, adding a level of abstraction, allows to avoid having to adapt the code to the "single browser standard", because even though today is a bit better (than years ago), basically there is still no real standard. jQuery添加了一个抽象级别,可以避免必须使代码适应“单一浏览器标准”,因为尽管今天(比几年前)要好一些,但是基本上还没有真正的标准。

So for instance, if you do $('#mydate') jquery will retrieve that element in every browser (using IE getElementById etc...) and you don't need anymore to worry about your code to be cross-browser. 因此,例如,如果您执行$('#mydate'),jquery将在每个浏览器中检索该元素(使用IE getElementById等),而您无需担心代码会跨浏览器了。

$('#mydate').val() //assuming it to be an input, will retrieve the value and then you can work with your own logic. $('#mydate')。val()//假设它是输入,将检索该值,然后您就可以使用自己的逻辑了。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript .split函数不适用于所有浏览器 - JavaScript .split function does not work in all browsers 如何获得preventDefault在所有浏览器上都可以使用? - How can I get preventDefault to work for all browsers? Javascript-如何获取所有浏览器名称? - Javascript - How do i get all the browsers name? javascript如何在FireFox中完美地工作,而在其他浏览器中却根本不工作? - How can javascript work perfectly in FireFox, but not work at all in other browsers? 我写了一个选项卡控件,但无法在所有浏览器中都使用CSS,该如何解决? - I wrote a tab control but I cannot get the css to work in all browsers, how do I fix it? Javascript:如何在此脚本中使split()函数起作用 - Javascript: How do I get the split() function in this script to work 如何获得此JavaScript /时刻代码以与美国和英国的日期格式一起使用? - How do I get this JavaScript / moment code to work with both US and UK date formats? 这在IE中有效,但在chrome中不起作用。 如何使它在所有浏览器中都能正常工作? - This works in IE but not chrome. How do I get it to work in all browsers? Javascript日期格式不适用于所有浏览器 - Javascript Date Format not working in all browsers 哪种Javascript日期格式与所有浏览器都兼容? - Which Javascript date format is compatible with all browsers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM