JavaScript JavaScript — slice time string to return HH:MM javascript slice time complexity,slice time javascript

I have a time/date string that is returned like so:

"5/31/2016, 2:23:33 PM". I need only the HH:MM and am/pm, so 2:23 PM

I am able to split the date off but not sure how I can remove the colon and seconds.

time = time.split(",")[0]; 
//time = " 2:23:33 PM";

thanks

Answer:1

If starting with a date/time string and desiring to end with a time string, of the same format, consider the following regular expression with two capture groups:

var dateStr = "5/31/2016, 2:23:33 pm";
var m = dateStr.match(/(\d{1,2}:\d{2}):\d{2}\s+?(AM|PM)/i)
//                      ^-- 1                    ^-- 2
if (m) {
   console.log(m[1] + " " + m[2].toUpperCase()); // "2:23 PM"
}

Otherwise, if ever needing to deal with a proper date/time object I'd consider moment.js - and specify the appropriate conversion format.

I would not use new Date(nonISO8601String) for parsing as it is unreliable across browsers/localizations.

Answer:2

I suggest using moment.js to parse the string and then output as a formatted string like this

var input = "5/31/2016, 2:23:33 PM";
var result = moment(input);

console.log(result.format("hh:mm a"));
//output: 02:23 pm

Fiddle

Answer:3

Update

As others have mentioned, although this answer does work as expected, that may not be the case across all browsers and locales the same.

My recommendation would preferably to use one of the popular date libraries like Moment.js, which could be argued as the de facto standard at the present time for handling this.

Using a Date Object

While this is entirely feasible through a series of splits, regular expressions, etc, it may simply be easier to just use some of Javascript's built-in Date functionality (assuming that your format isn't going to vary wildly).

You could consider writing a function to parse your date as a Date object and then formatting it accordingly :

function formatDateStringAsTime(input){
  var date = new Date(input);
  // Determine if it is AM or PM
  var ampm = date.getHours() >= 12 ? ' PM' : ' AM';
  // Output what you need
  return (date.getHours() % 12) + ':' + date.getMinutes() + ampm;
}

Additionally if you want to ensure you have two digits (i.e. zero padded values), you can use a bit of slicing to change that :

// Using this approach will pad your hours and minutes with zeros if necessary
return ('0' + (date.getHours() % 12)).slice(-2) + ':' + ('0' + date.getMinutes()).slice(-2) + ampm;

Example

formatDateStringAsTime("5/31/2016, 2:23:33 PM"); // yields 2:23 PM 
var input = "5/31/2016, 2:03:33 PM";
input = formatDateStringAsTime(input);
console.log(input);

function formatDateStringAsTime(input) {
  var date = new Date(input);
  // Determine if it is AM or PM
  var ampm = date.getHours() >= 12 ? ' PM' : ' AM';
  return ('0' + (date.getHours() % 12)).slice(-2) + ':' + ('0' + date.getMinutes()).slice(-2) + ampm;
}

Consider Date-specific Libraries

As others have mentioned, if you are going to be performing this behavior frequently within your application, you might consider using a library like Date.js or Moment.js, which are designed for exactly this.

Answer:4

You can do it in two lines using this regex:

var a = new Date("5/31/2016, 2:23:33 PM");

var b = a.toLocaleTimeString().replace(/:\d{2}\s/,' ');
console.log(b)
Answer:5

I've come to the realization that since promises in ECMAScript 6 allow for synchronous coding of asynchronous functions, for every promise-laden piece of code there's a synchronous corollary. For ...

I've come to the realization that since promises in ECMAScript 6 allow for synchronous coding of asynchronous functions, for every promise-laden piece of code there's a synchronous corollary. For ...

i'm using this JS code https://kolber.github.io/audiojs/docs/ and I was wondering if i can load and skipt to a specific point of the audio track. of if there is any other way to do it using an ...

i'm using this JS code https://kolber.github.io/audiojs/docs/ and I was wondering if i can load and skipt to a specific point of the audio track. of if there is any other way to do it using an ...

I have written code to return a new array containing only the unique elements of two seperate arrays. example: [1,2,3] and [1,2] will return 3. I have working code, however I would just like an ...

I have written code to return a new array containing only the unique elements of two seperate arrays. example: [1,2,3] and [1,2] will return 3. I have working code, however I would just like an ...

  1. javascript return array from function
  2. javascript return array of objects
  3. javascript return array index
  4. javascript return array without last element
  5. javascript return array value
  6. javascript return array as string
  7. javascript return array length
  8. javascript return array from foreach
  9. javascript return array undefined
  10. javascript return array without first element
  11. javascript return array key value
  12. javascript return array of object values
  13. javascript return array after push
  14. javascript return array of properties
  15. javascript return array range
  16. javascript return array without duplicates
  17. javascript return array from promise
  18. javascript return array of arrays
  19. javascript return array without element
  20. javascript return array with item removed

function chart(){ Barchart(); Linechart(); function Barchart(){ } function Linechart(){ } } Chart() is main parent function. Inside I have two chart ...

function chart(){ Barchart(); Linechart(); function Barchart(){ } function Linechart(){ } } Chart() is main parent function. Inside I have two chart ...

  1. call function inside function python
  2. call function inside function
  3. call function inside class python
  4. call function inside function javascript
  5. call function inside for loop javascript
  6. call function inside another function python
  7. call function inside class javascript
  8. call function inside useeffect
  9. call function inside react component
  10. call function inside iframe
  11. call function inside object javascript
  12. call function inside function c++
  13. call function inside class c++
  14. call function inside string javascript
  15. call function inside function powershell
  16. call function inside html
  17. call function inside module.exports
  18. call function inside lambda python
  19. call function inside bash script
  20. call function inside promise