0

I have this time in a variable called lastdate in javascript.

 07:31:00    

How can I show AM or PM using format specifier in Javascript..Like I use in php "%p" . Is there any method to use in javascript. Any help is much appreciated.

Aamir
  • 2,173
  • 1
  • 29
  • 58
  • 3
    use moment.js?. – mehulmpt Mar 15 '17 at 13:23
  • 1
    You would have to do it manually. You can get the hours from a Date object using `getHours()` and them work out the period from that. Alternatively you could use a library which does this for you, Like Moment.js or Date.js – Rory McCrossan Mar 15 '17 at 13:23
  • @MehulMohan Yes I'm reffering to moment js but I'm confused how to use it for my problem.. https://momentjs.com/docs/#/displaying/format/ – Aamir Mar 15 '17 at 13:25
  • You can use JavaScript toLocaleString() Method var time = new Date(); time = time.toLocaleString('en-US', { hour: 'numeric',minute:'numeric', hour12: true }); – Arun Mar 15 '17 at 13:29
  • if you looking for exact php date function for javascript, you can check this >> http://locutus.io/php/datetime/date/ or another datetime functions >> http://locutus.io/php/datetime they write a lot of php function for js – Fal Mar 15 '17 at 13:48

2 Answers2

0

Assuming that your time string will be in 24H format:

1.) Split time string into array.

2.) Create new Date object.

3.) Map the array to create integers. (this can also be done on each variable in the below method)

4.) Set the hours, minutes, and seconds to values of the generated array.

5.) Convert Date object to the local time string and use a Regex to create your new time string. (NOTE: toLocaleTimeString() may behave differently based on location)

/* Variable Defaults */
var timeString = '07:31:00';
var parts = timeString.match(/(\d{2}):(\d{2}):(\d{2})/);
var dateObject = new Date();

/* Cast `parts` as Integer */
Object.keys(parts).map(function(key, index) {
   parts[key] = parseInt(parts[key]);
});

/* Set Hours/Minutes/Seconds */
dateObject.setHours(parts[1]);
dateObject.setMinutes(parts[2]);
dateObject.setSeconds(parts[3]);

/* Output New String */
var newTimeString = dateObject.toLocaleTimeString().replace(/([\d]+:[\d]{2})(:[\d]{2})(.*)/, '$1$3')

/* Console Result */
console.log(newTimeString);
Daerik
  • 4,167
  • 2
  • 20
  • 33
  • Wow... Overkill – mplungjan Mar 15 '17 at 13:43
  • @mplungjan OP requested a method in JavaScript to accomplish this. This is what I gave them. Besides, the accepted answer for the "duplicated question" is far more complex. I guess I could have been ignorant and avoided their question all together and suggested they use some plugin. – Daerik Mar 15 '17 at 13:49
  • 1
    `var parts = '07:31:00'.split(":"), d = new Date(); d.setHours(parts[0],parts[1],parts[2],0); console.log(d.toLocaleString('en-US',{hour: 'numeric',minute: 'numeric',hour12: true}));` – mplungjan Mar 15 '17 at 14:31
-1

If you can bring in an external library, I'd recommend moment.js.

https://momentjs.com/

Then you can specify what you want with:

var lastdate = "07:31:00"
moment(lastdate, 'HH:mm:ss').format('LTS');  // 07:31:00 AM

Or if you want to be more explicit:

var lastdate = "07:31:00"
moment(lastdate, 'HH:mm:ss').format('HH:mm:ss A');  // 07:31:00 AM

Where the A means AM/PM. See https://momentjs.com/docs/#/displaying/format/.

user3432422
  • 1,359
  • 1
  • 11
  • 13
  • suppose if time is in variable lastdate..for ex: var lastdate = "07:31:00"; how can I apply moment for that variable ?? – Aamir Mar 15 '17 at 13:26
  • You can pass a string to be parsed into the moment function and format it as before - `moment(lastdate, "HH:mm:ss").format("LTS")`. – user3432422 Mar 15 '17 at 13:37
  • I've updated my answer to take the time from a variable rather than the current time. – user3432422 Mar 15 '17 at 15:05