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
Related
This question already has answers here:
javascript - Age calculation
(14 answers)
Closed 7 months ago.
I am trying through a date that in js returns me the years and months that have passed, but I only get it to show me the days
$("#nacimiento").change(function(){
var nacimiento=$("#nacimiento").val();
var fechaInicio = new Date(nacimiento).getTime();
var fechaFin = new Date('<? echo date("Y-m-d");?>').getTime();
var diff = fechaFin - fechaInicio;
var tiempo=diff/(1000*60*60*24);
console.log(diff/(1000*60*60*24));
document.getElementById('meses').innerHTML=tiempo;
});
You could use the Luxon package for this and create two DateTime objects and get the difference between them in Months from the Interval object and work from there.
eg
const fromUser = DateTime.fromString(nacimiento)
const fromPhp = DateTime.fromString('<? echo date("Y-m-d");?>')
const diff = Interval.fromDateTimes(fromUser, fromPhp);
const diffMonths = diff.length('months')
console.log(diffMonths)
another approach without an external package might be just using getMonth() and getFullYear() example:
const fromUser = new Date(nacimiento)
cnost fromPhp = new Date('<? echo date("Y-m-d");?>')
const months = fromUser.getMonth() -
fromPhp.getMonth() +
12 * (fromUser.getFullYear() - fromPhp.getFullYear())
This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 10 months ago.
I'm trying to create a date with the format 'mmddyyyy'. I've noticed I can pass a four digit string, but when I use the format above, it says invalid date. How can I go about this with pure js?
let dateString = '01012022'
let d = new Date(dateString)
maybe you want something like that
let dateString = '01012022'
let [match, dd, mm, yyyy] = dateString.match(/(\d{2})(\d{2})(\d{4})/)
// iso format
let d = new Date(`${yyyy}${mm}${dd}`)
Seems like you have to split your string into pieces then use common Date() constructor with separated day, month and year
//Your string with date
let dateString = '01012022';
//extracting 2 first numbers as m (month), 2 second as d (day) and the last 4 as y (year)
let {d,m,y} = /(?<m>\d{2})(?<d>\d{2})(?<y>\d{4})/.exec(dateString).groups;
//then create new date using constructor
let date = new Date(y,m,d);
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());
This question already has answers here:
How to convert dd/mm/yyyy string into JavaScript Date object? [duplicate]
(10 answers)
Closed 2 years ago.
I have this code to add a reminder to a calendar
function addCalendarEvent(eventDate, eventTitle){
let dateObj = new Date(eventDate);
let calendarId = "me#gmail.com";
let cal = CalendarApp.getCalendarById(calendarId);
let event = cal.createAllDayEvent(eventTitle, dateObj)
event.addEmailReminder(420)
}
The eventDate is passed into the function as a string in the format dd/MM/YYYY but the output from let dateObj = new Date(eventDate); is in american format. ie. 02/10/2020 comes out as Mon Feb 10 2020 00:00:00 GMT+0000 (Greenwich Mean Time). Any help correcting this would be great. Dates give me an absolute headache
In the end I decided that just working with date objects from the off would be easiest
This will give you the date object of the string in the format dd/MM/YYYY
function ddmmyyyytFormatterWithTime(date) {
var dateDiv = date.split('/');
var date = new Date();
date.setDate(dateDiv[0]);
date.setMonth(--dateDiv[1]); // -- since it starts from 0
date.setFullYear(dateDiv[2]--);
return date;
};
function ddmmyyyytFormatter(date) {
var dateDiv = date.split('/');
var date = new Date();
date.setHours(0, 0, 0, 0); // to reset time
date.setDate(dateDiv[0]);
date.setMonth(--dateDiv[1]); // -- since it starts from 0
date.setFullYear(dateDiv[2]);
return date;
};
var date = ddmmyyyytFormatter("10/02/2020");
var dateWithTime = ddmmyyyytFormatterWithTime("10/02/2020");
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).