sparkle
sparkle

Reputation: 7390

Date format on Mobile browser

std = new Date("2012-06-22 00:05:00");
std_string = std.getHours() + ":" + std.getMinutes();

On mobile browser (Android, Blackberry 7) it show: "Nan:Nan", why?

How could I fix that?

"2012-06-22 00:05:00" come from a http WebService and is MYSQL datetime format

Upvotes: 0

Views: 2073

Answers (2)

Zakaria
Zakaria

Reputation: 15070

Did you try to create your object like this :

var dateWS = "2012-06-22 00:05:00";
var date = dateWS.split(" ")[0].split("-");
var time = dateWS.split(" ")[1].split(":");

var dateObj = new Date(date[0],date[1],date[2],time[0],time[1],time[2]);
var std_string = dateObj .getHours() + ":" + dateObj .getMinutes();
alert(std_string);

I Jsfiddled it so you can try it directly on your browser : http://jsfiddle.net/PyGgb/

EDIT : I made a test on my Android 2.3 and it's working :

enter image description here

Upvotes: 2

stay_hungry
stay_hungry

Reputation: 1448

I think problem is '-' . Yous can use '/' instead of '-' ..

replace your "2012-06-22 00:05:00" '-' to '/'.

   var temp = "2012-06-22 00:05:00".replace('-','/');
   var std = new Date(temp);
   var std_string = std.getHours() + ":" + std.getMinutes();
   alert(std_string);

Upvotes: 0

Related Questions