MomentJS - Tutorialspoint
MomentJS
About the Tutorial
MomentJS is a JavaScript library which helps is parsing, validating, manipulating and displaying date and time in JavaScript in a very easy way. MomentJS can be used directly inside a browser and also with Node.js. Working with dates and time using JavaScript can be quite challenging, specifically if you have lots of manipulation to be done with dates. MomentJS comes with lots of features that eases your work with date and time.
Audience
This tutorial is designed for software programmers who want to learn the basics of MomentJS and its programming concepts in simple and easy way. This tutorial will give you enough understanding on various functionalities of MomentJS with suitable examples.
Prerequisites
This tutorial is designed keeping in mind that its readers have a basic understanding of HTML, CSS, and JavaScript. If you are new to these concepts, we recommend you to go through relevant tutorials and gain an understanding on them, before you proceed with this tutorial.
Copyright &Disclaimer
?Copyright 2018 by Tutorials Point (I) Pvt. Ltd. All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher. We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or in this tutorial, please notify us at contact@
i
Table of Contents
MomentJS
About the Tutorial...........................................................................................................................................i Audience .........................................................................................................................................................i Prerequisites ...................................................................................................................................................i Copyright &Disclaimer ....................................................................................................................................i Table of Contents...........................................................................................................................................ii
1.MOMENTJS ? OVERVIEW........................................................................................................1
Features .........................................................................................................................................................1
2.MOMENTJS ?ENVIRONMENT SETUP ...................................................................................... 3
Method 1: Using MomentJS File in Browser ...................................................................................................3 Method 2: Using Node.js................................................................................................................................6 Method 3: Using Bower .................................................................................................................................6
3.MOMENTJS ? INTRODUCTION................................................................................................8
MomentJS and RequireJS...............................................................................................................................8 MomentJS and TypeScript..............................................................................................................................9
4.MOMENTJS ? PARSING DATE AND TIME...............................................................................13
Parsing Date.................................................................................................................................................13 Now .............................................................................................................................................................14 String ...........................................................................................................................................................14 Object ..........................................................................................................................................................19 Date .............................................................................................................................................................19 Array ............................................................................................................................................................ 20 Unix Timestamp ...........................................................................................................................................20 Moment Clone .............................................................................................................................................21 UTC ..............................................................................................................................................................22
ii
MomentJS
parseZone ....................................................................................................................................................23 Creation Data...............................................................................................................................................24 Defaults .......................................................................................................................................................25
5.MOMENTJS - DATE VALIDATION...........................................................................................26
Parsing Flags ................................................................................................................................................26
6.MOMENTJS - GETTER/SETTER .............................................................................................. 28
Millisecond ..................................................................................................................................................30 Second .........................................................................................................................................................31 Minute .........................................................................................................................................................31 Hour ............................................................................................................................................................. 32 Date of Month .............................................................................................................................................33 Day of Week ................................................................................................................................................33 Day of week (Locale) ...................................................................................................................................34 ISO Day of week ...........................................................................................................................................35 Day of Year ..................................................................................................................................................36 Week of Year ...............................................................................................................................................37 Week of Year (ISO) ......................................................................................................................................37 Month .......................................................................................................................................................... 38 Quarter ........................................................................................................................................................39 Year .............................................................................................................................................................40 Week Year....................................................................................................................................................40 Week Year (ISO) ...........................................................................................................................................41 Weeks in Year ..............................................................................................................................................41 Weeks in Year (ISO)......................................................................................................................................42 Get ............................................................................................................................................................... 42 Set................................................................................................................................................................ 44 Maximum ....................................................................................................................................................44
iii
MomentJS
Minimum .....................................................................................................................................................45
7.MOMENTJS - MANIPULATE DATE AND TIME ........................................................................46
Methods to Manipulate Date and Time .......................................................................................................46 Add ..............................................................................................................................................................46 Subtract .......................................................................................................................................................51 Start of Time ................................................................................................................................................54 End of Time ..................................................................................................................................................55 Local ............................................................................................................................................................55 UTC ..............................................................................................................................................................56 UTC offset ....................................................................................................................................................56
8.MOMENTJS ? FORMATTING DATE AND TIME.......................................................................58
Methods to Format Date and Time ..............................................................................................................58 Format .........................................................................................................................................................59 Time from now ............................................................................................................................................64 Time from X .................................................................................................................................................65 Time to now.................................................................................................................................................67 Time to X......................................................................................................................................................67 Calendar Time ..............................................................................................................................................68 Difference ....................................................................................................................................................69 Unix Timestamp (milliseconds) ....................................................................................................................71 Unix Timestamp (seconds) ...........................................................................................................................71 Days in Month..............................................................................................................................................72 As JavaScript Date........................................................................................................................................72 Output .........................................................................................................................................................73 As Array .......................................................................................................................................................73 As JSON........................................................................................................................................................73 As ISO 8601 String ........................................................................................................................................74
iv
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.