Angular How to serialize Date in format yyyy-MM-dd in json request

angular httpclient date serialization
how to pass date in json format

I have an object who has a property 'birthDate' of type Date. On screen, I should display the birthday in format DD/MM/YYYY. Now I use bootstrap bsDatePicker. But in the database, it is stored in the format YYYY-MM-DD(type string). So to submit the modification, I should convert the birthday to this format.

Can you tell me how to serialize the Date(DD/MM/YYYY) to the format YYYY-MM-DD in the JSON request?

Do you have any good idea to optimize the conversion without any change in the database?

Thank you


First you are going to create a function that accepts a string that contains the date. Then convert it. At the end of the function I have some ternary code because if you don't add that part, the date could be 2018-4-2 and you want it 2018-04-02

dateFormater(date:string):string{    
   const today = new Date(date);        
   const day = today.getDate()+1; 
   const month = today.getMonth()+1;
   const year = today.getFullYear();

   return year + "-" + (month<10 ? ("0" + month) : month)+ "-" + (day<10 ? ("0" + day) : day);    
 }

What is the best way to store a date/time in JSON?, Can you tell me how to serialize the Date(DD/MM/YYYY) to the format YYYY-MM-​DD in the JSON request? Do you have any good idea to  Here is a quick format from JSON date format to required date format using jQuery: For a JSON date string like: /Date(1339439400000)/ try: var newFormattedDate = $.datepicker.formatDate('mm/dd/yy', new Date(Date(your_JSON_date_string))); and it will result in date like this: 09/14/2012


If its a string, you could just extract the relevant data and returning the correct format, needed to be submit in your json. A possible helper function:

    formatDate():string{    
        const today = new Date();        

        const day = today.getDate(); 
        const month = today.getMonth()+1; // this will return 0-11 so you will need to add 1 to make it, 1-12
        const year = today.getFullYear();

        return year+"-"+month+"-"+day;    
    }

this will return "2018-10-19".

The “right” JSON Date Format, as strings in a format that is widely used. Reserve 12 bits for YYYY until year 4096-1. Reserve 4 bits for MM because (0-11)is enough. Reserve 5 bits for DD because day of the month is (1-31). Here it is clearly shown that the value of the date of birth comes from the Web API with serialized format. JSON can't automatically deserialize the data. For doing this serialization we need to create a regular expression and convert the date value into proper format. For doing that, we will define a function for converting the datetime value


Thank you for all your anwser, this is what i did:

I map the origin object to the request type with reflxion, for the date:

if(item[key] instanceof Date) {
  value = this.dataPipe.transform(item[key], 'yyyy-MM-dd').toString();
}else {
  value = item[key].toString();
}

From post man(rest service) how to send json date(string format) to , Can you tell me how to serialize the Date(DD/MM/YYYY) to the format YYYY-MM-​DD in the JSON request? Do you have any good idea to optimize the  Date to format either as Date object, milliseconds (string or number) or various ISO 8601 datetime string formats (e.g. yyyy-MM-ddTHH:mm:ss.sssZ and its shorter versions like yyyy-MM-ddTHH:mmZ, yyyy-MM-dd or yyyyMMddTHHmmssZ). If no timezone is specified in the string input, the time is considered to be in the local timezone.


In this post I show how JSON date serialization works, a few approaches JSON has become a rather prevalent serialization format in recent years. you by a library of some sort like jQuery, or AngularJs or any other framework. The only thing we're sure of, is that the 10 first characters are yyyy-mm-dd. I am having a little bit of trouble creating my Angular 2 form and converting the submitted data into JSON format for the use of submitting it to my API. I am looking for something that works very similarly to this example: $.fn.serializeObject = function


Related posts: – Angular 6 – KeyValue Pipe – *ngFor Loop through Object, Map Mar 12, 2018 · I chose to display the date in the en_US format MM/dd/yy (e. to serialize Date and DateTime to JSON without losing information Nov 5, 2014. into the Flutter framework, introduced Dart, and showed how to call the native  Note that I literally copy pasted your question title Convert Json date date into this 'yyyy-MM-dd' format in angular 2 . 0. How to convert string date to notification date format in Ionic 2. 0. Angular2 formating text input into dd-MM-yyyy. 0. how t


Formats date to a string based on the requested format . sssZ and its shorter versions like yyyy-MM-ddTHH:mmZ, yyyy-MM-dd or yyyyMMddTHHmmssZ). When you do a .json() that will not transform a string (even if it's in some date format) to an actual date object. It'll keep being a string. so you need to do new Date(herePassTheString) – arg20 Apr 17 '16 at 19:32