Why setDate() is giving the final date in milliseconds in nodejs? [duplicate] - javascript

This question already has answers here:
setDate() returns number 1603240915215 instead of a date
(2 answers)
Closed 2 years ago.
I a trying to save a date to the nextMonth. For that I am first setting the month to next 30 days. But the final output date it is giving me in milliseconds.
I want the date in GMT format strictly.
What can I do for that?
var snm = new Date();
snm = snm.setDate(snm.getDate() + 30);
console.log("snm = "+ snm);

Try this
var snm = new Date();
snm.setDate(snm.getDate() + 30)
console.log("snm = "+ snm.toString());

Related

new Date() always read dd/mm/yyyy as mm/dd/yyyy(month first) hence day and month values get mixed resulting in NaN/Error [duplicate]

This question already has answers here:
How to convert dd/mm/yyyy string into JavaScript Date object? [duplicate]
(10 answers)
Why does Date.parse give incorrect results?
(11 answers)
Closed 3 years ago.
When converting date for example:
var dateObj = new Date("10/01/2019");
console.log(dateObj);
returns Tue Oct 01 2019 00:00:00 i.e. it takes in the day as month and likewise with the month value
How to make new Date() to take dd/mm/yyyy ??
Answer is here: https://stackoverflow.com/a/33299764/6664779
(From original answer) We can use split function and then join up the parts to create a new date object:
var dateString = "23/10/2019"; // Oct 23
var dateParts = dateString.split("/");
// month is 0-based, that's why we need dataParts[1] - 1
var dateObject = new Date(+dateParts[2], dateParts[1] - 1, +dateParts[0]);
document.body.innerHTML = dateObject.toString();

Add 30 days to a Current date - JS [duplicate]

This question already has answers here:
How to add days to Date?
(56 answers)
Closed 5 years ago.
I want to add 30 days to current date and get in a format.
Im confused to add a 30 to a date and get new.
Its pure JS Solution needed.
Format : June 10, 2017
var date = new Date(); // Now
date.setDate(date.getDate() + 30); // Set now + 30 days as the new date
console.log(date);
Im assuming you are using datejs?
var newDate = Date.today().add(30).days();
try following:
var yourDate = new Date();
var numberOfDaysToAdd = 30;
console.log(yourDate.setDate(yourDate.getDate() + numberOfDaysToAdd));

How do I convert date in normal format like "2017-03-03 09:46:22.103"? [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 5 years ago.
Consider:
var myDate = $scope.today;
var previousDay = new Date(myDate);
previousDay.setDate(myDate.getDate() - 1);
var date = previousDay; // 2017-03-04 09:46:22.103 +5.30 GMT
How do I get to only have "2017-03-03 09:46:22.103"?
You can use
var requiredDateFormat = previousDay.toLocaleDateString() + " " + previousDay.toLocaleTimeString();
You can use new Date().toJSON();. It will return "2017-03-04T04:55:50.786Z".

Adding time to a date in javascript [duplicate]

This question already has answers here:
Incrementing a date in JavaScript
(19 answers)
Closed 6 years ago.
I have this script;
showDiff();
function showDiff() {
var date1 = new Date("2016/03/14 00:00:00");
var date2 = new Date();
var diff = (date2 - date1);
var diff = Math.abs(diff);
var result;
if (diff > 432000000) {
result = 100 + "%";
} else {
result = (diff/4320000) + "%";
}
document.getElementById("showp").innerHTML = result;
document.getElementById("pb").style.width = result;
setTimeout(showDiff,1000);
}
Now I want to get exactly one week added to date1 when atleast one week has passed since that time. That date has to be saved so that one week later, another week can be added to date1. So basically every monday there has to be one week added to date1. How do I this?
The Date object has both a getDate() and setDate() function (date referring to the day of the month, no the full calendar date), so it's really as simple as getting a Date object and setting its date to +7 days from itself.
Example:
var weekFromNow = new Date();
weekFromNow = weekFromNow.setDate(weekFromNow.getDate()+7);
Just to clarify, the Date object contains a full calendar date and time, with its date property referring just to the day in the month (also different from its day property, which is the day of the week).

UK Date issue in java script date object [duplicate]

This question already has answers here:
JavaScript date objects UK dates
(6 answers)
Closed 9 years ago.
I have the below code. If we try to retrieve the day, month and year in UK server setup. The date object returns an incorrect value.
var startDate = "30/08/2013";
var d = new Date(startDate);
alert(d.getFullYear()); //2015
Please help me
Use moment.js if you want better control of parsing:
var startDate = "30/08/2013";
var m = moment(startDate,"DD/MM/YYYY");
alert(m.year()); //2013
Try passing the date as a list of arguments instead:
var startDate = "30/08/2013"; // Would also work with 30-08-2013
var startDateArray;
if(startDate.contains('/'))
startDateArray = startDate.split('/');
else if (startDate.contains('-'))
startDateArray = startDate.split('-');
else if (startDate.contains('.'))
startDateArray = startDate.split('.');
var d = new Date(startDateArray[2], startDateArray[1]-1, startDateArray[0]); // Month is 0 based, that's why we are subtracting 1

Categories

Resources