John Cooper
John Cooper

Reputation: 7631

Extracting time from the date respone

Date.parseWeird = Date.prototype.parseWeird = function (d) {
    return new Date(parseInt(d.replace(/[^\-\d]/g, ""), 10));
};

var newDate = Date.parseWeird("\/Date(1317847200000-0400)\/");

document.write(newDate);

This returns me Thu Oct 06 2011 02:10:00 GMT+0530 (India Standard Time)

I want to extract this 02:10:00 and convert this into time like this 9:30 P.M or A.M

Upvotes: 0

Views: 83

Answers (2)

Freesnöw
Freesnöw

Reputation: 32143

Can't you just try something like this?

http://jsfiddle.net/wXvzt/

<h4>It is now  
<script type="text/javascript">
<!--
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
if (minutes < 10){
minutes = "0" + minutes
}
document.write(hours + ":" + minutes + " ")
if(hours > 11){
document.write("PM")
} else {
document.write("AM")
}
//-->
</script>
</h4>

Upvotes: 3

JMax
JMax

Reputation: 26591

To display time with AM / PM, you can use this kind of script:

var hours = newDate.getHours()
var minutes = newDate.getMinutes()
if (minutes < 10){
  minutes = "0" + minutes
}
document.write(hours + ":" + minutes + " ")
if(hours > 11){
  document.write("PM")
} else {
  document.write("AM")
}

Upvotes: 1

Related Questions