Home > Net >  Parse to string for data before sending request
Parse to string for data before sending request

Time:12-02

I know the data before sending will be parsed to string from any type. So where is this process done and how does it happen? I had this question when I had a problem regarding the format for a Date type in javascript. Specifically, I want to send data that has a field of type Date and I want to format it before sending (example: 29-12-2022). However, I always get a result like 2022-12-29T17:00:00.000Z. I can convert the interface of data from Date to String. However, I don't want to do that

CodePudding user response:

You should use functions provived default by Date class:

  1. Use toLocaleDateString function

const d = new Date();
let text = d.toLocaleDateString();
console.log(text);

  1. Manually concatenate date, month, year strings:

let d = new Date();
let mm = d.getMonth()   1;
let dd = d.getDate();
let yy = d.getFullYear();
console.log(`${dd}-${mm}-${yy}`);

  • Related