5

I have a jQuery script that receives a string in milliseconds inside a parameter, like this:

params.tweetDate='77771564221';

What I need to do is to create a jQuery function that will be able to format this milliseconds string in a USA time, like 10.00 AM or 10.00 PM.

Is there a jQuery function that is able to do this?

Please help.

Thanks

Reporter
  • 3,897
  • 5
  • 33
  • 47
DiegoP.
  • 45,177
  • 34
  • 89
  • 107
  • what does tweetDate represent? is it milliseconds since midnight or since 1969 (I think this is where Date will start counting from if provided by milliseconds) ?? – redmoon7777 Jan 10 '12 at 10:01
  • @redmoon7777: The normal values are either seconds or milliseconds since The Epoch (Jan 1, 1970 at midnight GMT). I noted in my answer that the above appears to be neither, as using it as milliseconds since The Epoch gives us a date in June 1972 (long before Twitter), and using it as seconds since The Epoch gives us a date in June 4434. :-) So the first thing will indeed be to find out what that value represents. – T.J. Crowder Jan 10 '12 at 10:08

5 Answers5

8

There is Date object in pure javascript, no jQuery needed. http://www.javascriptkit.com/jsref/date.shtml

Example:

var time = new Date(params.tweetDate),
h = time.getHours(), // 0-24 format
m = time.getMinutes();
// next just convert to AM/PM format (check if h > 12)
YuS
  • 2,025
  • 1
  • 15
  • 24
3

No, there's no jQuery function for this. You can use

  • JavaScript's own Date object, using the getHours() and getMinutes() functions, handling the AM/PM thing yourself (e.g., hours >= 12 is PM), padding out the minutes with a leading 0 if minutes is less than 10, etc. Also note that if hours is 0, you want to make it 12 (because when using the AM/PM style, you write midnight as "12:00 AM", not "0:00 AM").
  • DateJS, an add-on library that does a huge amount of date stuff (although sadly it's not actively maintained)
  • PrettyDate from John Resig (the creator of jQuery)

To use just about any of those, first you have to turn that "milliseconds" value into a Date object. If it's really a "milliseconds" value, then first you parse the string into a number via parseInt(str, 10) and then use new Date(num) to create the Date object representing that point in time. So:

var dt = new Date (parseInt(params.tweetDate, 10));

However, the value you've quoted, which you said is a milliseconds value, seems a bit odd — normally it's milliseconds since The Epoch (Jan 1, 1970), which is what JavaScript uses, but new Date(parseInt("77771564221", 10)) gives us a date in June 1972, long before Twitter. It's not seconds since The Epoch either (a fairly common Unix convention), because new Date(parseInt("77771564221", 10) * 1000) gives us a date in June 4434. So the first thing to find out is what that value actually represents, milliseconds since when. Then adjust it so it's milliseconds since The Epoch, and feed it into new Date() to get the object.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

Here is a function for you:

function timeFormatter(dateTime){
var date = new Date(dateTime);
if (date.getHours()>=12){
    var hour = parseInt(date.getHours()) - 12;
    var amPm = "PM";
} else {
    var hour = date.getHours(); 
    var amPm = "AM";
}
var time = hour + ":" + date.getMinutes() + " " + amPm;
console.log(time);
return time;
}

You may call the function in any approach like:

var time = timeFormatter(parseInt("2345678998765"));
tika
  • 7,135
  • 3
  • 51
  • 82
1

take a look at timeago: this is a jquery plugin used exactly for this purposes.

Orentet
  • 2,353
  • 1
  • 17
  • 28
  • I do not understand it very well. Could you please provide a sample of how it will work with a milliseconds string? – DiegoP. Jan 10 '12 at 09:58
  • +1 but this seems to be more about using the "ago" style rather than just formatting in the U.S. time style as the OP requested. – T.J. Crowder Jan 10 '12 at 10:04
1

Using T.J.'s solution this is what I ended up with.

var date = new Date(parseInt("77771564221", 10));
var result = new Array();
result[0] = $.datepicker.formatDate('DD, M, d, yy', date);
result[1] = ' ';
if (date.getHours() > 12) {
    result[2] = date.getHours() - 12;
} else if (date.getHours() == 0 ) {
    result[2] = "12";
} else {
    result[2] = date.getHours();
}
result[3] = ":"
result[4] = date.getMinutes();

if (date.getHours() > 12) {
    result[5] = " pm";
} else {
    result[5] = " am";
}

console.log(result.join(''));
badMonkey
  • 1,687
  • 1
  • 22
  • 23