[英]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发明以来,它便可以在所有浏览器中使用。
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.