Hi guys,
Having a headache here…hope someone already come with similar stuff.
Lets see. I have a WS which returns a date in format
AppState.dataWS_Detalhe.dataEntrega = “2016-05-30T18:00:00.000-0300”
Now I call a JS function I declared
PassouDate(AppState.dataWS_Detalhe.dataEntrega.toString())
The JS function is:
function PassouDate(date1)
{
var dataAtual = new Date();
var dataWS = new Date(date1);
if(dataWS > dataAtual)
return 1; //Ainda nao passou
else
return 0 + ' ' + dataAtual + ' vs ' + dataWS + ' from ' + date1; //Passou
}
Nothing from the other world…
Function always return 0 because the output is:
0 Mon May 30 2016 11:38:23 GMT-0300 vs Invalid Date from 2016-05-30T18:00:00.000-0300
However if I execute the same exact logic and function from a browser it works as expected…returns 1 or 0 as date is treated as valid…
I already tried calling the function with
PassouDate(AppState.dataWS_Detalhe.dataEntrega)
And have same exact problem…any clue why on earth??? Or a workaround?
It seems that the AppState.dataWS_Detalhe.dataEntrega.toString() method has the following format:
YYYY-MM-DDTHH:mm:ss.sss-hhmm
Try to change to
YYYY-MM-DDTHH:mm:ss.sss-hh:mm (adding colon between hh and mm).
Seems the proper string notation for date as specified by ECMAScript Language (referenced on http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.15 - section 15.9.1.15 “Z is the time zone offset specified as “Z” (for UTC) or either “+” or “-” followed by a time expression HH:mm”)
Well, it is just a guess but I think that there’s no harm to try it…
Tried:
2016-05-30T11:10:00.000
2016-05-30T11:10:00.000-03:00
Same problem…thanks for the idea
Have tried even simpler date formats and nothing, always get an Invalid Date message.
Funnier
Did this:
x = new Date().toISOString()
x = ‘2016-05-30T18:31:19.000Z’
Yay! yeah? I found the valid format…a Z you would say. So lets test!
y = new Date(x);
y = Invalid date
XD
This really sucks
again, testing all this on browser works without problems.
More news:
Found 3 valid formats so far:
2009,11,11
Fri Mar 25 2015 09:56:24 GMT+0100 (Tokyo Time)
Wed Mar 25 2015 09:56:24 GMT+0100
Can’t find how to do the 2009,11,11 to include hh and mm
SO…
Valid formats so far:
2009,11,11 OK
Fri Mar 25 2015 09:56:24 GMT+0100 (Tokyo Time) OK
Wed Mar 25 2015 09:56:24 GMT+0100 OK
Sat Jun 09 2007 17:46:21 OK
Jun 09 2007 17:46:21 OK
06 09 2007 17:46:21 OK
Will use the last one.
What about use a different constructor instead of Date(String)? Even the Spidermonkey Javascript Engine (which MCP uses) does not recommend using this constructor:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
The issue is that the WS JS date format is valid…I can’t ask them to modify it as is a universal WS and works on other components.
I can’t do nothing with date functions with that Date returned…so can do only parsing. finally went with 06 09 2007 17:46:21
Nice info about the JS engine used
I was searching that
I don’t remember where I found it, but last time I was looking for it, I saw that MCP uses SpiderMonkey 1.7 (maybe it has been updated already)
Found it on VP MCP 8.1.x release notes (https://docs.genesys.com/Special:Repository/gvp_mcp81rn.html?id=da7f5c20-055a-4dd5-bc20-cadd6f900d2e)
Mozilla SpiderMonkey JavaScript engine in the Next Generation VoiceXML Interpreter has been upgraded to version 1.7.
Using MCP 8.5
Had the hope that an upgrade would help too but no luck
Enviado de meu E6633 usando Tapatalk
So, I’m far from being a good developer, but find below an attempt for a function that parses the date in ISO8601 (such as your WS return example).
function parseISO8601(strISO8601Date) {
var dateRegExp = /^([0-9]{4})-([0-9]{2})-([0-9]{2})(T([0-9]{2}):([0-9]{2}):([0-9]{2})\.([0-9]{3})(Z|[-+][0-9]{2}:?[0-9]{2}))?$/i;
var match = dateRegExp.exec(strISO8601Date);
var year = parseInt(match[1]);
var month = parseInt(match[2]);
var day = parseInt(match[3]);
var hasTime = match[4];
var hour;
var minute;
var second;
var mils;
var tz;
var mydate;
if (hasTime && (typeof hasTime != 'undefined')) {
hour = parseInt(match[5]);
minute = parseInt(match[6]);
second = parseInt(match[7]);
mils = parseInt(match[8]);
tz = match[9];
mydate = new Date(year, month-1, day, hour, minute, second, mils);
if ("Z" === tz.toUpperCase()) {
mydate.setTime(mydate.getTime() - mydate.getTimezoneOffset() * 60000);
} else {
tz = tz.replace(":","");
var tzhour = parseInt(tz.substr(1,2));
var tzmin = parseInt(tz.substr(3,2));
var tzOffset = (tz.substr(0,1) === "+") ? ((tzhour * 3600000) + (tzmin * 60000)) : -((tzhour * 3600000) + (tzmin * 60000));
var diff = -tzOffset - (mydate.getTimezoneOffset() * 60000);
mydate.setTime(mydate.getTime() + diff);
}
} else {
mydate = new Date(year, month-1, day);
}
return mydate;
}
If you can try it out on MCP and post results that would be great as I don’t have a lab ready for this kind of testing (although I have tested against FireFox without problems).
Nice
but I ended up doing something simpler, need to think on guys who will support this
function PassouDate(date1)
{
//06 09 2007 17:46:21 OK
//date1 = 2016-04-18T03:00:00.000+0000
var YYYY = date1.substr(0,4);
var MM = date1.substr(5,2);
var DD = date1.substr(8,2);
var HHMMSS = date1.substr(11,8);
date1 = YYYY + ' ' + MM + ' ' + DD + ' ' + HHMMSS;
var dataAtual = new Date();
var dataWS = new Date(date1);
if(dataWS > dataAtual)
return 1; //Ainda nao passou
else
return 0; //Passou
}