Please anyone share the code to subtract month from this format date(2020,7,24, 11,0). For example,current date with time is (2020,7,24, 11,0) and i get (2020,6,24, 11,0).can anybody help me how to do?
Asked
Active
Viewed 1,566 times
2 Answers
1
If you have object date, it's simple:
const d = new Date(2020,7,24, 11,0);
// Remember in Date months starts from 0
console.log(d);
// Substract 1 month
d.setMonth(d.getMonth() - 1);
// Log
console.log(d);
Or if you need, you can parse manually
// Set raw
const rawDate = '2020,7,24, 11,0';
console.log(rawDate);
// Convert your date
const [year, month, day, hour, minute] = rawDate.split(',');
// Set date
var d = new Date();
d.setFullYear(year);
d.setMonth(month-1); // Month starts from 0
d.setDate(day);
d.setHours(hour);
d.setMinutes(minute);
console.log(d);
// Substract month
d.setMonth(d.getMonth() - 1);
// Log
console.log(d);
// If you need to convert back to your format
const rawDateModified = `${d.getFullYear()},${d.getMonth()+1},${d.getDate()}, ${d.getHours()},${d.getMinutes()}`;
console.log(rawDateModified);
Updated answer to show how you can pass string to Date object
// You get ds Date param as string
let ds = "2020,2,28,3,44";
// To pass it in Date object, you must
// convert it to array by splitting string
ds = ds.split(',');
// Then pass array to Date object with
// spread operator
const d = new Date(...ds);
// Log
console.log(d);

tarkh
- 2,424
- 1
- 9
- 12
-
i got Property 'setYear' does not exist on type 'Date'. this error? – Kapil Soni Jul 25 '20 at 12:34
-
Yes, you right, I've updated answer. setYear is deprecated, use setFullYear – tarkh Jul 25 '20 at 12:35
-
its woking...i have another question Like:Date format -- '2018, 07, 01, 10, 0' i have to remove 0 from only month day not from HH and mm.i tried replace(/\b0/g, '')but its remove 0 from all section.can you tell me how to do? – Kapil Soni Jul 26 '20 at 10:01
-
You can pass this kind of strings (like 07) to Date object, it will work. But if you need to make your string with no 0 in month or date, simplest way is to split your line by comma: `let [year, month, date, hour, min] = '2018, 07, 01, 10, 0'.split(',');`, then replace what you want i.e: `month = month.replace(/0(\d)/, '$1');`. Then put everything back to line. – tarkh Jul 26 '20 at 23:03
-
Yes it's working.can u tell me how to create this type format date- new Date(2020,2,28,3,44) – Kapil Soni Jul 28 '20 at 18:02
-
I didn't get your question - what's wrong with `const d = new Date(2020,2,28,3,44);` ? It creates proper date object. – tarkh Jul 28 '20 at 19:30
-
actually i received this string "2020,2,28,3,44" from backend side then at front end side i will convert ? – Kapil Soni Jul 29 '20 at 05:15
-
Oh, now I get you! Ok, first you need to split your string to array: `const ds = "2020,2,28,3,44".split(',');`, then pass this array to Date object like so: `const d = new Date(...ds);`. I'll update my answer for this – tarkh Jul 29 '20 at 09:04
-
ok bro i will try it i also tried at my side check my below question and check its correct or not: https://stackoverflow.com/questions/63102192/event-is-not-show-in-the-time-block-after-change-date-in-syncfusion-scheduler-li/63129292?noredirect=1#comment111668563_63129292 – Kapil Soni Jul 29 '20 at 09:15
-
i will try and i got Expected 0-7 arguments ,but got 0 error after Date(...ds); – Kapil Soni Jul 29 '20 at 09:30
-
What happened where are u – Kapil Soni Jul 30 '20 at 19:01
0
In Javascript Date objects months are starting from 0 and going to 11 instead of regular 1 to 12 months that we are used to. In order to format it in a better way, you can use plain Javascript.
let now = new Date();
console.log(now); //Sat Jul 25 2020 13:55:30 GMT+0200 (Central European Summer Time)
let normalValues =[now.getFullYear(),now.getMonth()+1,now.getDate(),now.getHours(),now.getMinutes()];
let formated = normalValues.join(",");
console.log(formated); //2020,7,25,13,55
//Explanation for current time 2020/7/25 14:05
now.getFullYear(); //returns 2020
now.getMonth(); //Returns 6, that is why we add one to it to make it 7
now.getDate(); //Returns 25
now.getHours(); //Returns 14
now.getMinutes(); //Returns 5

Ognjen
- 144
- 8