Format JSON Date String To Local DataTime Object Using JavaScript

Category > JAVASCRIPT || Published on : Wednesday, November 18, 2015 || Views: 9802 || onvert the returned JSON date string into a valid javascript object javascript object JSON date string into a valid javascript object


Introduction

Some conditions it is necessary to convert the returned JSON date string into a valid javascript object and then display in web page.

Description

In previous post I have explained MS-SQL Server Convert String to Proper Case (Camel Case) or Title Case Function Example, SQL Server Execute Stored Procedure from Another Stored Procedure with Parameters, Create table from json jQuery with detailed example, How to Store Hashtable in Session using asp.net and C# with example, Zoom Feature for Query Text and Results Text in SQL Server Management Studio 2012, How to make How to make blinking/flashing text with CSS3 and jQuery, and many more articles.

Now I will explain How to Format JSON Date String To Local DataTime Object Using JavaScript

So follow the steps to learn Format JSON Date String To Local DataTime Object Using JavaScript
function formatJSONDate(jsonDate) {
            var newDate = new Date(parseInt(jsonDate.substr(6)));
            return newDate = newDate.toLocaleFormat('%d-%b-%Y');
        }

Below is the code to call above javascript function to format jsonDate

     var jsonDate = "/Date(1313151600000)/";
        alert(formatJSONDate(jsonDate));

Complete JavaScript Function:-

<script type="text/javascript">
        var jsonDate = "/Date(1313151600000)/";
        alert(formatJSONDate(jsonDate));
        function formatJSONDate(jsonDate) {
            var newDate = new Date(parseInt(jsonDate.substr(6)));
            return newDate = newDate.toLocaleFormat('%d-%b-%Y');
        }
    </script>

Following is the complete HTML source code that I’ve used for this demonstration to format json date with example:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Format JSON Date String To Valid Local DataTime Object by: www.SourceCodeHub.com</title>
    <script type="text/javascript">
        var jsonDate = "/Date(1313151600000)/";
        alert(formatJSONDate(jsonDate));
        function formatJSONDate(jsonDate) {
            var newDate = new Date(parseInt(jsonDate.substr(6)));
            return newDate = newDate.toLocaleFormat('%d-%b-%Y');
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    </form>
</body>
</html>

Output ScreenShot:-

Conclusion:

So, In this tutorial we have learned, Format JSON Date String To Local DataTime Object Using JavaScript

Download Source Codes