function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vishwas B NVishwas B N 

format date value

Hi Team,
 
I need to convert the date format '2021-07-20' I received from Front End to '2021-07-20 00:00:00' (Date time format). Can you help how can I do it in JS.
 
Thank you.
Best Answer chosen by Vishwas B N
ravi soniravi soni
hy Vishwas,
try following js code
const currentDate = new Date();

const currentDayOfMonth = currentDate.getDate();
const currentMonth = currentDate.getMonth(); // Be careful! January is 0, not 1
const currentYear = currentDate.getFullYear();

const dateString = currentDayOfMonth + "-" + (currentMonth + 1) + "-" + currentYear;
dateString;
//output : "19-7-2021"

If you want like '2021-07-20' then apply following code
const currentDate = new Date();

const currentDayOfMonth = currentDate.getDate();
const currentMonth = currentDate.getMonth(); // Be careful! January is 0, not 1
const currentYear = currentDate.getFullYear();

const dateString = currentYear + "-" + (currentMonth + 1) + "-" + currentDayOfMonth ;
dateString;
//output : "2021-07-20"


let me know if it helps you and marking it as best answer.
Thank you
 

All Answers

ravi soniravi soni
hy Vishwas,
try following js code
const currentDate = new Date();

const currentDayOfMonth = currentDate.getDate();
const currentMonth = currentDate.getMonth(); // Be careful! January is 0, not 1
const currentYear = currentDate.getFullYear();

const dateString = currentDayOfMonth + "-" + (currentMonth + 1) + "-" + currentYear;
dateString;
//output : "19-7-2021"

If you want like '2021-07-20' then apply following code
const currentDate = new Date();

const currentDayOfMonth = currentDate.getDate();
const currentMonth = currentDate.getMonth(); // Be careful! January is 0, not 1
const currentYear = currentDate.getFullYear();

const dateString = currentYear + "-" + (currentMonth + 1) + "-" + currentDayOfMonth ;
dateString;
//output : "2021-07-20"


let me know if it helps you and marking it as best answer.
Thank you
 
This was selected as the best answer
ravi soniravi soni
hy Vishwas,
You must reply of this answer and if it helped you then you must mark it as best answer.
Thank You 
Veer