Working With Date Time Using JavaScript

Working with date and time is always a bit cumbersome and implementing it using JavaScript is a bit tedious. Then we got information about moment.js. It's a very good library for validating and parsing the date and time. It also has a feature to manipulate dates based on your local time, you can parse the date in UTC format also. You can get details about moment.js using http://momentjs.com/docs/.

I will create here a sample MVC web project and implement a moment.js. Please use the following procedure.

  • Create a Sample MVC web Project MomentJs
  • Add a Nuget Package reference for moment.js. After Installing it into your project/script folder the moment.js file is added.


  • Add a person class library project MomentJs.Model.
  • Add a Person Class.


  • Now add the following entry into your App_start/BundleConfig.cs:
  1. bundles.Add(new ScriptBundle("~/bundles/moment").Include(  
  2.   
  3. "~/Scripts/moment.js",  
  4.   
  5. "~/Scripts/moment-with-locales.js"));  
  • Add the following entry into the Views/Shared/_Layout.cshtml:
  1. @Scripts.Render("~/bundles/moment")    
Create a new Controller (SampleMomentController.cs) and a sample view Index.cshtml.
The following is the sample code of Index.cshtml.
I have used some methods of moment.js that I will explain later. Now the working environment is set for the magic of moment.js.
I will explain here some methods that I have used. You can get the details from the moment.jsdocs that I have said before.

Up Next
    Ebook Download
    View all
    Learn
    View all