0

What I want it is to get the output of the current time with Javascript. The output should be something similar as:

15:28:30 PM

And I got this using the following code:

var date = new Date();
document.write("Current time: " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds());

if (date.getHours() <= 12)
   document.write(" AM");
else
   document.write(" PM");

So the output that I get it is:

Current time: 3:0:16 AM

But I want to know if there is some faster or cleaner solution to solve this problem because I think my solution it is not good at all.

Is it possible to get the same behaviour with a better method or solution?

Thanks in advance!

Francisco Romero
  • 11,900
  • 18
  • 71
  • 151
  • 1
    Essentially a repeat of question: http://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date (where I'd go for the second answer) – monty Oct 07 '15 at 01:19

5 Answers5

3
var date = new Date();
var time = date.toLocaleString('en').split(', ').pop();

This will give you the exact format you are looking for. Although I would go with a library like Moment.js or Date.js. Tons of options with those.

mikkelrd
  • 403
  • 2
  • 9
  • With your solution I am getting the date and not the time. This is the output that I get with your solution: `10/7/2015`. – Francisco Romero Oct 07 '15 at 01:27
  • Now it works! But please can you explain a bit more what do you use in your solution? Or why the output expected comes with that? Thank you! – Francisco Romero Oct 07 '15 at 01:35
  • 2
    toLocaleString() returns a string with a language sensitive date representation. We specify 'en' for english. Then we chain some methods. The split method will divides a string at the specified character set, in this case comma + space, and puts them in an array, so in this case `"10/6/2015, 7:46:19 PM"` becomes `["10/6/2015", "7:46:19 PM"]`. Lastly, the pop method simply removes the first item in an array, in this case the date string, leaving only the time string. It seemed like you were looking for a very concise solution, which this is, but not very flexible. Upvote if helpful, please – mikkelrd Oct 07 '15 at 01:50
  • Great explanation! Clear and simply. Thank you very much! – Francisco Romero Oct 07 '15 at 09:22
  • But I still have a question. If you see here: http://www.w3schools.com/jsref/jsref_pop.asp it says that pop method removes the last element of an array. Why it removes the first one? – Francisco Romero Oct 07 '15 at 09:24
  • Sorry, I saw now that it returns the element that is being removed: `The pop() method removes the last element of an array, and returns that element.` – Francisco Romero Oct 07 '15 at 09:26
  • you are correct. i didn't explain that part very well – mikkelrd Oct 07 '15 at 16:21
1

It's like this:

var dt = new Date;
console.log(dt.toLocaleTimeString());
StackSlave
  • 10,198
  • 2
  • 15
  • 30
0

I see nothing wrong with your approach. However, if you want the flexibility of different formats, there's a library called moment.js which allows you to build and format dates.

moment().format('hh:mm:ss A'); // 12:00:00 AM
Joseph
  • 107,072
  • 27
  • 170
  • 214
  • I am totally new at `javascript` and I know that this will be a dumb question but... How can I use it on my javascript? Have I import something? I made my javascript on a notepad inside an html file that I runs on my browser. – Francisco Romero Oct 07 '15 at 01:12
  • And what should I download? There are a lot of files to download: https://cdnjs.com/libraries/moment.js. Thank you again! – Francisco Romero Oct 07 '15 at 01:23
  • @JosephtheDreamer: Would this code return `15:00:00 PM` or `03:00:00 PM`, bro ? – Mr Neo Oct 07 '15 at 01:28
  • @MrNeo The latter. `HH` is for 24-hour. `hh` is for 12-hour. You can remove the `A` to remove AM/PM. – Joseph Oct 07 '15 at 02:51
0

I think that you make right way. Only need modify a little to get your expectation:

var date = new Date();
int hour = date.getHours();
string abbr;

if (date.getHours() <= 12)
   abbr = " AM";
else {
   hour = hour + 12;
   abbr = " PM";
}
document.write("Current time: " + hour + ":" + date.getMinutes() + ":" + date.getSeconds() + abbr);
Mr Neo
  • 1,399
  • 3
  • 21
  • 39
  • Thanks for answer but I think it is worst clean than my solution. With my solution I can get the output expected without using a new variable. – Francisco Romero Oct 07 '15 at 01:31
  • @Error404: Ah, you're right. I did not read about `faster` and `cleaner`. So sorry, bro – Mr Neo Oct 07 '15 at 01:33
0
    var currentTime = new Date()
    var hours = currentTime.getHours()
    var minutes = currentTime.getMinutes()
    var seconds = currentTime.getSeconds()

    if (minutes < 10) {
        minutes = "0" + minutes
    }
    if (seconds < 10) {
        seconds = "0" + seconds
    }
    str += hours + ":" + minutes + ":" + seconds + " ";
    if(hours > 11){
        str += "PM"
    } else {
        str += "AM"
    }
    return str;
}

Found this thread

Community
  • 1
  • 1