snipt

Ctrl+h for KB shortcuts

JavaScript

Calculate Age from Date of Birth

1
2
3
4
5
function calcAge(dob) {
	var dateOfBirth = +new Date(dob);
	var age = ~~((Date.now() - dateOfBirth) / (31557600000));
	return age;
}
https://snipt.net/embed/3f1f816bb27b6de25aa819bba872868a/
/raw/3f1f816bb27b6de25aa819bba872868a/
3f1f816bb27b6de25aa819bba872868a
js
JavaScript
5
2019-06-14T20:13:04
True
False
False
Feb 08, 2014 at 02:06 PM
/api/public/snipt/111976/
calculate-age-from-date-of-birth
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="kd">function</span> <span class="nx">calcAge</span><span class="p">(</span><span class="nx">dob</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-2"><a name="L-2"></a> <span class="kd">var</span> <span class="nx">dateOfBirth</span> <span class="o">=</span> <span class="o">+</span><span class="k">new</span> <span class="nb">Date</span><span class="p">(</span><span class="nx">dob</span><span class="p">);</span> </span><span id="L-3"><a name="L-3"></a> <span class="kd">var</span> <span class="nx">age</span> <span class="o">=</span> <span class="o">~~</span><span class="p">((</span><span class="nb">Date</span><span class="p">.</span><span class="nx">now</span><span class="p">()</span> <span class="o">-</span> <span class="nx">dateOfBirth</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="mi">31557600000</span><span class="p">));</span> </span><span id="L-4"><a name="L-4"></a> <span class="k">return</span> <span class="nx">age</span><span class="p">;</span> </span><span id="L-5"><a name="L-5"></a><span class="p">}</span> </span></pre></div> </td></tr></table>
"calculate age", "date of birth", age, dob, javascript, js

JavaScript

Capitalize String

1
2
3
function capitalize(str) {
	return str.charAt(0).toUpperCase() + str.slice(1);
}
https://snipt.net/embed/4da6e32d983ef5530df98ba966b3cc8c/
/raw/4da6e32d983ef5530df98ba966b3cc8c/
4da6e32d983ef5530df98ba966b3cc8c
js
JavaScript
3
2019-06-13T19:30:16
True
False
False
Feb 08, 2014 at 02:02 PM
/api/public/snipt/111975/
capitalize-string
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="kd">function</span> <span class="nx">capitalize</span><span class="p">(</span><span class="nx">str</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-2"><a name="L-2"></a> <span class="k">return</span> <span class="nx">str</span><span class="p">.</span><span class="nx">charAt</span><span class="p">(</span><span class="mi">0</span><span class="p">).</span><span class="nx">toUpperCase</span><span class="p">()</span> <span class="o">+</span> <span class="nx">str</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span> </span><span id="L-3"><a name="L-3"></a><span class="p">}</span> </span></pre></div> </td></tr></table>
capitalize, javascript, js, string

JavaScript

Get ID in js w/jQuery dom object return

$( document.getElementById("some_id") );
https://snipt.net/embed/a263aa5f5037f8d42f5005eb040cd348/
/raw/a263aa5f5037f8d42f5005eb040cd348/
a263aa5f5037f8d42f5005eb040cd348
js
JavaScript
1
2019-06-13T15:26:25
True
False
False
Apr 08, 2014 at 09:22 AM
/api/public/snipt/107720/
get-id-in-js-wjquery-dom-object-return
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="nx">$</span><span class="p">(</span> <span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="s2">&quot;some_id&quot;</span><span class="p">)</span> <span class="p">);</span> </span></pre></div> </td></tr></table>
element, get, id, jquery, js