JQuery formatting time

I have a jQuery script that gets a string in milliseconds inside a parameter, for example:

params.tweetDate='77771564221'; 

What I need to do is create a jQuery function that can format this millisecond string in the USA, for example 10.00 AM or 10.00 PM .

Is there a jQuery function that can do this?

Please, help.

thanks

+4
source share
5 answers

There is a Date object in pure javascript, no jQuery required. 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) 
+8
source

No, there is no jQuery function for this. you can use

  • JavaScript own Date object, using the functions getHours() and getMinutes() , independently processing AM / PM (for example, hours> = 12 - PM), postponing minutes with leading 0, if minutes are less than 10, etc. Also note that if the clock is 0, you want to do it 12 (because when using the AM / PM style, you write midnight as “12:00 AM” rather than “0:00 AM”).
  • DateJS , an additional library that makes a huge amount of date materials (although, unfortunately, it is not actively supported)
  • PrettyDate by John Rezig (creator of jQuery)

To use almost any of them, you first need to turn this millisecond value into a Date object. If this is really a millisecond value, first you parse the string into a number via parseInt(str, 10) , and then use new Date(num) to create a Date object representing this point in time. So:

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

However, the value you specified, the value in milliseconds, seems a little strange - it's usually milliseconds since The Epoch (January 1, 1970), which uses JavaScript, but new Date(parseInt("77771564221", 10)) gives us the date in June 1972, long before Twitter. This is not a second since The Epoch (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 really represents the value, milliseconds since. Then adjust it so that it lasts for milliseconds with The Epoch, and pass it to new Date() to get the object.

+3
source

Here is the function:

 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 can call the function in any approach, for example:

 var time = timeFormatter(parseInt("2345678998765")); 
+2
source

take a look at timeago : this is a jquery plugin that is used specifically for these purposes.

+1
source

Using TJ is what I came across.

 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('')); 
+1
source

Source: https://habr.com/ru/post/1390199/


All Articles