Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I dont think there is any built-in function for this, however it would not be complicated to achieve what you want.</p> <p><a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date" rel="nofollow">https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date</a></p> <p>First, create a date with 0 elapsed milliseconds:</p> <pre><code>var d = new Date(0); </code></pre> <p>Parse your time string and set the hours of the date accordingly:</p> <pre><code>var t = timeString.match(/(\d{1,2}):(\d{2}) ([AP]M)/), h = parseInt(t[1], 10), isAm = t[3] === 'AM', isMidnight = h === 12 &amp;&amp; isAm, isNoon = h === 12 &amp;&amp; !isAm; d.setUTCHours(isMidnight? 0 : h + (isAm || isNoon? 0 : 12), parseInt(t[2], 10)); </code></pre> <p>Use the <code>getTime</code> function to get the milliseconds:</p> <pre><code>console.log(d.getTime()); //10800000 </code></pre> <p>Here's a re-usable function:</p> <pre><code>function getTimeMilliseconds(timeString) { var t = timeString.match(/(\d{1,2}):(\d{2}) ([AP]M)/), h = parseInt(t[1], 10), isAm = t[3] === 'AM', isMidnight = h === 12 &amp;&amp; isAm, isNoon = h === 12 &amp;&amp; !isAm; return new Date(0).setUTCHours(isMidnight? 0 : h + (isAm || isNoon? 0 : 12), parseInt(t[2], 10)); } getTimeMilliseconds('3:00 AM'); </code></pre> <p><em>Note that you don't really need the <code>Date</code> object to achieve this, but I use it because it does the math for you, however you could always do <code>hours * 3600000 + minutes * 60000</code>.</em></p> <pre><code>function getTimeMilliseconds(timeString) { var t = timeString.match(/(\d{1,2}):(\d{2}) ([AP]M)/), h = parseInt(t[1], 10), isAm = t[3] === 'AM', isMidnight = h === 12 &amp;&amp; isAm, isNoon = h === 12 &amp;&amp; !isAm; return (isMidnight? 0 : h + (isAm || isNoon? 0 : 12)) * 3600000 + parseInt(t[2], 10) * 60000; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload