python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
json datetime format in javascript jquery

json datetime format in javascript jquery

Hello everyone,

I'm going to share the code-sample for DateTime format in jquery JSON

In the 1st step, we get the DateTime using javascript, sample code as  given below.

 var createDate = new Date().getTime();


In the 2nd step, we create request body for input-data, sample code as given below.

var requestBody = { 
                                user: { 
                                           Id: 9, 
                                           CreatedDate: '/Date(' + createDate + ')/'
                                        } 
                             }; 

In the 3rd step, we convert to request body to JSON using JSON.stringify()
code sample as given below.

var JSONrequestBody=JSON.stringify(requestBody);

In the 4th step, we write to authenticate to services and call to JSON request.  


    var username = "user";
    var password = "[email protected]";

    var doAuthenticate = function (uid, pwd) {

        var token = user + ':' + password;
        var hashCode = btoa(token);
        return  'Basic ' + hashCode; 
    };


    $.ajax({

            url: 'https://192.168.2.10/Services/myService.svc/REST/saveUserDetail',
            type: 'POST',
            data: JSONrequestBody,
            beforeSend: function (xhr) {
                xhr.setRequestHeader("Authorization", doAuthenticate(uid, pwd));
            },
            async: runInBackground,
            contentType: 'application/json',
            success: successHandler,
            error: function (jqXHR, textStatus, errorThrown) {
                   $('#msgText').html(jqXHR.responseText);

            }

      });

Example for JSON datetime format in jquery

var  JSONDateTimeFormat= function()
{
    var createDate = new Date().getTime();
    var requestBody = { user: { Id: 9, CreatedDate: '/Date(' + createDate + ')/'} };
    var JSONrequestBody=JSON.stringify(requestBody);

        $.ajax({
            url: 'https://192.168.2.10/Services/myService.svc/REST/saveUserDetail',
            type: 'POST',
            data: JSONrequestBody,
            beforeSend: function (xhr) {
                xhr.setRequestHeader("Authorization", doAuthenticate(uid, pwd));
            },
            async: runInBackground,
            contentType: 'application/json',
            success: successHandler,
            error: function (jqXHR, textStatus, errorThrown) {
                   $('#msgText').html(jqXHR.responseText);

            }
        });
       

       var uid= "user";
       var pwd= "[email protected]";
       var doAuthenticate = function (uid, pwd)

        {
             var token = user + ':' + password;
             var hashCode = btoa(token);
             return  'Basic ' + hashCode;
       };

};

ANIL SINGH

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - http://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
www.code-sample.com/. Powered by Blogger.
ASK Questions