web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Convert date value to JSON format

(0) ShareShare
ReportReport
Posted on by

Hi, I am trying to convert the createdon date to JSON format.

var createdOn = Xrm.Page.getAttribute("createdon").getValue();


This gives createdOn = Wed Jan 13 2016 14:01:49 GMT-0500 (Eastern Standard Time)

However, I need the JSON format (2016-01-13T14:01:49). The issue is that I am trying to get the created on date + 7 days, and using this info for a query.

Is there such method to convert this easily? I know that there is a way to add 7 days using the Date object in JScript, I am just not sure how to take this raw date value and convert to any reasonable format.

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Community Member Profile Picture
    on at

    I wrote a solution to this for anyone who needs it:

    function convertDateToJSON(rawdate) {
        var res = rawdate.toString().split(" ");
        var time = res[4].split(":");
    
        var tempdate = new Date();
        var formattedtime = new Date(tempdate.setDate(rawdate.getDate() + 7));
        formattedtime = new Date(formattedtime.setHours(time[0]));
        formattedtime = new Date(formattedtime.setMinutes(time[1]));
        formattedtime = new Date(formattedtime.setSeconds(time[2]));
    
        var parsed = formattedtime.toString().split(" ");
        var JSONformat = parsed[3] + "-" + getmonth(parsed[1]) + "-" + parsed[2] + "T" + parsed[4];
    
        return JSONformat;
    
    }
    function getmonth(month) {
        switch (month) {
            case "Jan": return "01"; break;
            case "Feb": return "02"; break;
            case "Mar": return "03"; break;
            case "Apr": return "04"; break;
            case "May": return "05"; break;
            case "Jun": return "06"; break;
            case "Jul": return "07"; break;
            case "Aug": return "08"; break;
            case "Sep": return "09"; break;
            case "Oct": return "10"; break;
            case "Nov": return "11"; break;
            case "Dec": return "12"; break;
        }
    }


    This converts the raw date you receive from using getValue on a date field and converts it to a JSON format, which then can be added to a query (such as OData).

  • nickmanny Profile Picture
    136 on at

    JSON does not know anything about dates. What .NET does is a non-standard hack/extension. The problem with dates in JSON and really JavaScript in general – is that there's no equivalent literal representation for dates. In JavaScript following Date constructor straight away converts the milliseconds since 1970 to Date as follows:

    var jsonDate = new Date(1297246301973);

    Then let's convert it to js format:

    var date = new Date(parseInt(jsonDate.substr(6)));

    The substr() function takes out the /Date( part, and the parseInt() function gets the integer and ignores the )/ at the end. The resulting number is passed into the Date constructor .

  • Guido Preite Profile Picture
    54,086 Moderator on at

    it's an old thread, however it's not required to do that kind of parsing

    when you do a getValue from a form attribute you get a JavaScript Date object and to convert to JSON you just use toJSON function, like this:

    var now = new Date();
    var nowJSON = now.toJSON();

    nickmanny your answer doesn't resolve the initial question and please don't copy&paste your response from here https://stackoverflow.com/a/10286228/2191473 and the link you left in your answer http://net-informations.com/jq/iq/jdate.htm

    Because this is not a .NET problem and the code you pasted actually doesn't work

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans