avatarJohn Au-Yeung

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1461

Abstract

ing:</p><div id="cb8d"><pre>dayjs.extend(utc)<span class="hljs-comment">;</span></pre></div><p id="ccca">Now we can call the <code>dayjs.utc</code> method to create a date object with the current date and time in UTC.</p><h1 id="0574">Clone a Date</h1><p id="c588">We can clone a Day.js date object with the <code>clone</code> method.</p><p id="00a0">For instance, we can write:</p><div id="0601"><pre><span class="hljs-keyword">const</span> dayjs = <span class="hljs-built_in">require</span>(<span class="hljs-string">"dayjs"</span>); <span class="hljs-keyword">const</span> a = <span class="hljs-title function_">dayjs</span>(); <span class="hljs-keyword">const</span> b = a.<span class="hljs-title function_">clone</span>(); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(a, b);</pre></div><p id="418f">We call <code>dayjs</code> to create date object <code>a</code> .</p><p id="5dea">Then we call <code>clone</code> on <code>a</code> and assign it to <code>b</code> to make <code>b</code> a clone of <code>a</code> .</p><h1 id="91dd">Validation</h1><p id="5249">To validate a date, we can use the <code>isValid</code> method.</p><p id="6c20">It returns <code>true</code> if the date is valid and <code>false</code> otherwise.</p><p id="38d4">For instance, we can write:</p><div id="c8b7"><pre>const dayjs <span class="hljs-operator">=</span> require(<span class="hljs-string">"dayjs"</span>)<span class="h

Options

ljs-comment">;</span> const isValid <span class="hljs-operator">=</span> dayjs().isValid()<span class="hljs-comment">;</span> console.log(isValid)<span class="hljs-comment">;</span></pre></div><p id="35f9">We call <code>isValid</code> on a Day.js date object.</p><p id="b0b8">Since we called in a valid date, <code>isValid</code> is <code>true</code>.</p><h1 id="e101">Conclusion</h1><p id="c259">Day.js is a JavaScript library that lets us manipulate dates in our apps.</p><h1 id="46ff">In Plain English 🚀</h1><p id="50f3"><i>Thank you for being a part of the <a href="https://plainenglish.io"><b>In Plain English</b></a> community! Before you go:</i></p><ul><li>Be sure to <b>clap</b> and <b>follow</b> the writer ️👏<b>️️</b></li><li>Follow us: <a href="https://twitter.com/inPlainEngHQ"><b>X</b></a><b> | <a href="https://www.linkedin.com/company/inplainenglish/">LinkedIn</a> | <a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw">YouTube</a> | <a href="https://discord.gg/in-plain-english-709094664682340443">Discord</a> | <a href="https://newsletter.plainenglish.io/">Newsletter</a></b></li><li>Visit our other platforms: <a href="https://stackademic.com/"><b>Stackademic</b></a><b> | <a href="https://cofeed.app/">CoFeed</a> | <a href="https://venturemagazine.net/">Venture</a> | <a href="https://blog.cubed.run">Cubed</a></b></li><li>More content at <a href="https://plainenglish.io"><b>PlainEnglish.io</b></a></li></ul></article></body>

Manipulating Dates with Day.js — Parsing and Validation

Photo by Artem Kniaz on Unsplash

Day.js is a JavaScript library that lets us manipulate dates in our apps.

In this article, we’ll look at how to use Day.js to manipulate dates in our JavaScript apps.

Parsing Date to UTC Date

We can use the utc plugin to parse a date into a UTC date.

For instance, we can write:

const dayjs = require("dayjs");
const utc = require("dayjs/plugin/utc");
dayjs.extend(utc);
const dt = dayjs.utc().format();
console.log(dt);

to import the Day.js with the utc plugin.

Then we can use the plugin by writing:

dayjs.extend(utc);

Now we can call the dayjs.utc method to create a date object with the current date and time in UTC.

Clone a Date

We can clone a Day.js date object with the clone method.

For instance, we can write:

const dayjs = require("dayjs");
const a = dayjs();
const b = a.clone();
console.log(a, b);

We call dayjs to create date object a .

Then we call clone on a and assign it to b to make b a clone of a .

Validation

To validate a date, we can use the isValid method.

It returns true if the date is valid and false otherwise.

For instance, we can write:

const dayjs = require("dayjs");
const isValid = dayjs().isValid();
console.log(isValid);

We call isValid on a Day.js date object.

Since we called in a valid date, isValid is true.

Conclusion

Day.js is a JavaScript library that lets us manipulate dates in our apps.

In Plain English 🚀

Thank you for being a part of the In Plain English community! Before you go:

Programming
Web Development
Technology
Software Development
JavaScript
Recommended from ReadMedium