Please enable Javascript for better experience...
Convert string date into JSON format
By Big | Jul 12, 2017 | In Tips | Update: Jul 12, 2017 | Total Views [ 172 ]
Taged In
(0 Like)
Rate

 In below section you will find a way to convert string date into JSON date format.

The Problem

I was looking for a way so that I can convert a date in string format into JSON date format. Below code can help you achieving this.

The Solution

Copy below code snippet to your js file. It will definitely work for you. In below code pass input parameter in "dd-mm-yyyy" format. If your date value is in another format, you will need to do little modification in below code snippet.

//input date is in dd-mm-yyyy format
function convertToJSONDate(inputDate){
  var dateArr = inputDate.split("-");
  //method Date(yyyy,mm,dd)
  var dt = new Date(dateArr[2],dateArr[1],dateArr[0]);
  var newDate = new Date(Date.UTC(dt.getFullYear(), dt.getMonth(), dt.getDate(), dt.getHours(), dt.getMinutes(), dt.getSeconds(), dt.getMilliseconds()));
  return '/Date(' + newDate.getTime() + ')/';
};

Calling method

var date = convertToJSONDate("25-12-2017");
console.log("Date in JSON format is:"+ date);

Output: /Date(1516838400000)/

Hope it can help you...

Share this

About the Author

Big
Rahul Jha (Big)
Founder, Developer dotnet-concept.com
Has working experience in different phases of Software Development Life Cycle (SDLC) in CMS, Gaming, Health Care and Financial Services domain using Agile pattern. Working experience in Design patterns, ASP.NET, MVC, Windows application, WCF, ADO.NET, SQL Server and Test Driven Development (TDD) environment with JQuery, JavaScript, N-Unit, Entity Frameworks, LINQ, Code Refactoring and Business Objects Models.

 
Please SignUp/Login to comment...

Or comment as anonymous...
* Name
* Email ID
Comment