Strange split-line behavior in javascript

I am trying to do something relatively simple. I have a date in this dd / MM / yyyy format, for example:

var newDate = "β€Ž11β€Ž/β€Ž06β€Ž/β€Ž2015";

And I want to convert it to a date.

This code only works in Chrome and Firefox:

new Date(newDate)

In IE11, I get Nan

So, I am trying to do this:

var parts = newDate.split("/");
var year = parts[2].trim();
var month = parts[1].trim();
var day = parts[0].trim();
var dt = new Date(Number(year), Number(month) - 1, Number(day));

Which should work, but I came across a very strange error.

If you try this code:

function myFunction() {
  var newDate = "β€Ž11β€Ž/β€Ž06β€Ž/β€Ž2015";
  var parts = newDate.split('/');
  var year = parts[2].trim();

  var a = year;
  var b = Number(year);
  var c = parseInt(year, 10);
  var d = parts;
  var n = a + "<br>" + b + "<br>" + c + "<br>" + d;
  document.getElementById("demo").innerHTML = n;
}
<p>Click the button to see the parse error.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>
Run codeHide result

Then in IE it adds a mystery symbol, and it prints Γ½2015, and in chrome displays ?2015.

Actually the meaning of the parts in IE is: Γ½11Γ½,Γ½06Γ½,Γ½2015 In Chrome:?11?,?06?,?2015

I can’t understand where these secret characters came from! My source line is just"β€Ž11β€Ž/β€Ž06β€Ž/β€Ž2015"

There seems to be no way to make something so simple, like parsing an integer from a simple string.

Fiddle , , , Number("2015") Nan, ?

UPDATE

, , :

var date = new Date();
var dateToSave = date.toLocaleDateString();

IE.

Chrome Firefox U+200E , IE !

toLocaleDateString() kendo.toString(selectedValue, "dd/MM/yyyy") .

moment.js : moment(selectedValue).format("DD/MM/YYYY"), - IE11 U+200E.

+4
3

"β€Ž11β€Ž/β€Ž06β€Ž/β€Ž2015".split('').map(function(s){return s.charCodeAt(0)}) ( Unicode) - : [8206, 49, 49, 8206, 47, 8206, 48, 54, 8206, 47, 8206, 50, 48, 49, 53]

U + 200E . , .

, .

: "11/06/2015".

+8

Scimonster , -ASCII-, : , . , split trim :

function go() {
  var newDate = "β€Ž11β€Ž/β€Ž06β€Ž/β€Ž2015";
  var expr = /\d+/g;
  var parts = newDate.match(expr);
  
  document.getElementById("result").innerHTML =
    "Parts: " + parts +
    "<br>Year: " + parts[0] +
    "<br>Month: " + parts[1] +
    "<br>Day: " + parts[2];
}
<button onclick="go()">Try me</button>
<div id="result"/>
Hide result

, "β€Ž11β€Ž/β€Ž06β€Ž/β€Ž2015" "11-6-2015" junk11/06/2016junk.

+4

For older browsers, you need to write a function that will parse the string.

The next function will create a method Date.fromISO- if the browser can get the correct date from the ISO string, its own method is used. Some browsers got this partly correct, but returned the wrong time zone, so just checking for NaN might not.

(function(){
var D= new Date('2011-06-02T09:34:29+02:00');
if(!D || +D!== 1307000069000){
    Date.fromISO= function(s){
        var day, tz,
        rx=/^(\d{4}\-\d\d\-\d\d([tT ][\d:\.]*)?)([zZ]|([+\-])(\d\d):(\d\d))?$/,
        p= rx.exec(s) || [];
        if(p[1]){
            day= p[1].split(/\D/);
            for(var i= 0, L= day.length; i<L; i++){
                day[i]= parseInt(day[i], 10) || 0;
            };
            day[1]-= 1;
            day= new Date(Date.UTC.apply(Date, day));
            if(!day.getDate()) return NaN;
            if(p[5]){
                tz= (parseInt(p[5], 10)*60);
                if(p[6]) tz+= parseInt(p[6], 10);
                if(p[4]== '+') tz*= -1;
                if(tz) day.setUTCMinutes(day.getUTCMinutes()+ tz);
            }
            return day;
        }
        return NaN;
    }
}
else{
    Date.fromISO= function(s){
        return new Date(s);
    }
}
})()

The result will be:

var start_time = '2012-06-24T17:00:00-07:00';
var d =  Date.fromISO(start_time);
var month = d.getMonth();
var day = d.getDate();

alert(++month+' '+day); // returns months from 1-12

Below function works for IE 8 and below.

// parse ISO format date like 2013-05-06T22:00:00.000Z
 function convertDateFromISO(s) {
 s = s.split(/\D/);
  return new Date(Date.UTC(s[0], --s[1]||'', s[2]||'', s[3]||'', s[4]||'', s[5]||'', s[6]||''))
  }

You can test as below:

   var currentTime = new Date(convertDateFromISO('2013-05-06T22:00:00.000Z')).getTime();
   alert(currentTime);
0
source

All Articles