-1

I have this datetime string

"2017-05-12T22:35:58.873912"

I want it to be

"22:35:58"

So the expected format should be HH:mm:ss , how can I do that? Thanks in advance.

Shawn L.
  • 159
  • 2
  • 2
  • 9
  • There are [many examples of how to do this on the internet](https://www.google.com.au/search?q=javascript+get+time+for+datetime+string&oq=javascript+get+time+for+datetime+string&aqs=chrome..69i57j35i39l2j0l3.17183j0j7&sourceid=chrome&ie=UTF-8), at least 2 already on this site. Please at least attempt some research before posting a question. – Jon P May 17 '17 at 00:47

1 Answers1

0

Firstly, create a variable that stores this.

var currentTime = new Date();

Then, you're going to want to use the .getHours, .getMinutes and .getSeconds methods to get each one of those, then display them in a string.

console.log("The current time is " +
    currentTime.getHours() + ":" +
    currentTime.getMinutes() + ":" +
    currentTime.getSeconds()
);

Please note that this will display the time in military time, in case you are someone outside of that time specification.

  • I want to change `"2017-05-12T22:35:58.873912"` to `"22:35:58"`. Not get the current timestamp. – Shawn L. May 17 '17 at 00:45
  • @ShawnL. You could use the .slice method to cut off everything, refer to [this](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice) – UghThatGUyAgain May 17 '17 at 00:51