I'm working on a NodeJS Projects and I get wrong Date values. And I don't get what I am doing wrong.
var d = new Date(results[1].timestamp);
console.log(results[1].timestamp); // 2016-05-10T13:29:47 <- this is right (stored at my DataBase)
console.log(d.getDate()); //10
console.log(d.getFullYear()); //2016
console.log(d.getMonth()); //4
console.log(d.getDay()); //2
console.log(d.getHours()); //15
console.log(d.getMinutes()); //29
console.log(d.getSeconds()); //47
So Month, Day and Hours are wrong.
I see these results in google chrome at my Mac
Thanks for helping
A few errors here:
getMonth returns a 0 based month. That is May is 04.
getDay returns the day of the week. I guess you want getDate
the date is parsed as UTC and getHour is according to the locale. So the hour might be different from what you want (but right here it seems to be "exact", as is it's the same value than inputted).
A tip for your next problems: Have a look at some documentation. For example the MDN.
The getDay() method returns the day of the week (from 0 to 6) for the specified date.
The getMonth() method returns the month (from 0 to 11) for the specified date, according to local time.
The getHours() method returns the hour (from 0 to 23) of the specified date and time.
getDay() function returns the Day of the date i.e. from sunday to saturday(0 to 6)
getMonth() function returns month frome January to December (0 to 1) so here you need to add 1 to correctly get the value
and I am afraid you misinterpreted getHours() result, because if I check for the mentioned date it gives me 13
Related
const date = new Date("2020-03-20T12:45:52.793Z");
console.log(date.getDate() + 1);
it shows 21 instead of 3
I'm new in javascript. the operation += does not works
regarding.
This has nothing to do with the operator (+, +=) used. Instead, it is to do with the function you used. From w3schools:
The getDate() method returns the day of the month (from 1 to 31) for the specified date.
See the following snippet:
const date = new Date("2020-03-20T12:45:52.793Z");
console.log(date.getDate());
As you can see, date.getDate() outputs 20, the day of the month you specified. So it is no surprise that adding 1 to this value will get you 21.
Given that you expect the output to be 3, I assume you want to get the month of the date.
To do this, you need to use getMonth and not getDate.
And as you have already correctly noticed, you need to add 1 to the month number if you want to get the result 3, because January starts at 0.
Again, the w3schools definition tells you what you need to know:
The getMonth() method returns the month (from 0 to 11) for the specified date, according to local time.
Note: January is 0, February is 1, and so on.
const date = new Date("2020-03-20T12:45:52.793Z");
console.log(date.getMonth() + 1);
I have a date and I want to substract today of this date. This is my example:
date.format('YYYY-MM-DD')
"2018-04-07"
moment().format('YYYY-MM-DD')
"2018-04-06"
date.diff(moment(), 'days')
0
The diff call returns 0 instead of 1. What is wrong here?
By default, moment#diff will truncate the result to zero decimal
places, returning an integer. If you want a floating point number,
pass true as the third argument. Before 2.0.0, moment#diff returned a
number rounded to the nearest integer, not a truncated number.
To see the full value, pass true as the third parameter:
now.diff(date, 'days', true)
If you want to compare just dates, then use:
var now = moment().startOf('day');
which will set the time to 00:00:00 in the local time zone. And compare with date
Use fromNow() function to understand why you are getting 0 instead of 1. It is very straight-forward.
Do like this :
moment(date).fromNow();
It will give you number of days passed if time is greater than 24 hours otherwise it will give to time in hours. e.g. 2 hours ago, 23 hours etc.
Below is example:
console.log(moment("2018-04-06", "YYYY-MM-DD").fromNow());
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
So you can see it is returning 18 hours ago (as of now) which is less than 24hours i.e. 1 day.
I would suggest to use fromNow instead of diff to get exact difference.
Hope now it makes clear to you.
moment() returns a full moment including time, so it's doing a diff from today, including time, to midnight of the 7th of April, which isn't a full day.
I was also facing the same issue. So after a few research on StackOverflow and moment.js documentation I came up with this solution. Works perfectly for me.
const date1 = "2021-05-12T06:30:00.000Z"
const date2 = "2021-05-18T06:30:00.000Z"
const day1 = moment((moment(date1).format("YYYY-MM-DD")).split("-"))
const day2 = moment((moment(date2).format("YYYY-MM-DD")).split("-"))
const diff = day2.diff(day1,'days')
The code below shows the day, month and year as:
6
3
116
Which is obviously wrong dates.
var date= new Date();
var day=date.getDay();
var month=date.getMonth();
var year=date.getYear();
console.log( day );
console.log( month );
console.log( year );
function next() {
};
Fiddle.
getDay() returns the day of the week from 0-6 for Sun-Sat,
getMonth() returns the month from 0-11, so 3 means April,
getYear() has been deprecated and replaced with getFullYear() which should be used instead.
It just looks like all of these functions do something different than what you were expecting.
To get day of the month from 1-31: getDate(),
To get month like you were expecting, just add 1: getMonth() + 1
You're querying the wrong functions and misunderstanding the output.
getDay() returns the day of the week.
getMonth() returns the month, but January starts with 0.
getYear() returns the year minus 1900
You are probably looking for:
getDate(), getMonth()+1, getFullYear()
Encoding in javascript for months from is 0-11 (not 1-12).
For year, you could use getFullYear() instead of getYear()
"Date.prototype.getFullYear()
Returns the year (4 digits for 4-digit years) of the specified date according to local time."
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
I fetched the iso week from a date by using moment's isoWeek function.
moment(new Date(2015,11,28)).isoWeek() //output 53
I fetched the iso week year from the same date by using moment's isoWeekYear function.
moment(new Date(2015,11,28)).isoWeekYear() //output 2015
But when I gave the same outputs to the input of moment function it results a different date.
moment().isoWeek(53).isoWeekYear(2015).isoWeekday(0).toDate() //output Dec 28 2014
For other dates it is working correctly. Is there anything that I am missing in my code or it is a bug with Moment ?
here is a demo JSFiddle console.log("Iso Week :",moment(new Date(2015,11,28)).isoWeek());
console.log("Iso Year :",moment(new Date(2015,11,28)).isoWeekYear());
console.log("Date :", moment().isoWeek(53).isoWeekYear(2015).isoWeekday(0).toDate());
It might be the order you've given to the segments.
This works:
moment().isoWeekYear(2015).isoWeekday(1).isoWeek(53).toDate());
Check this out from Moment.js docs.
if you chain multiple actions to construct a date, you should start
from a year, then a month, then a day etc. Otherwise you may get
unexpected results, like when day=31 and current month has only 30
days (the same applies to native JavaScript Date manipulation), the
returned date will be 1st of the following month.
Also isoWeekDays go from 1 to 7. By setting 0 you were getting next week's Monday.
first you have to understand one thing.in your first operation whats happening here is,
moment(new Date(2015,11,28)).isoWeek() //output 53
while creating date using new Date() you have passed 11 as month.so what will happen is while creating date month will get incremented by 1.so the date will be 2015-12-28.so the week number is 53.
so for the 3rd operation you have passed the same result.so moment returned the correct date.
in your case if you want to pass month subtract that by 1 in your 1st operation.
moment(new Date(2015,10,28)).isoWeek();
now you will get the correct answer
Hi I have following code that is suppose to extract day,month and year part separately.
$scope.$watch('inlineDatepicker.date', function() {
alert('hey, myVar has changed!' +$scope.inlineDatepicker.date);
alert($scope.inlineDatepicker.date.getDay());
alert($scope.inlineDatepicker.date.getMonth());
alert($scope.inlineDatepicker.date.getUTCFullYear());
});
Problem with the code is I can extract year correctly but day and month do not extract correctly. I tried as well
alert($scope.inlineDatepicker.date.getUTCDady());
alert($scope.inlineDatepicker.date.getUTCMonth())
Still wrong day and month.
Please let me know how I can change it to get correct month and day values. Thanks. Here is the plunker for it.
http://plnkr.co/edit/8v75gsz8ODUrTfu8S0sh?p=preview
To get day of month, use getUTCDate()
Month is zero based, so 0 means January, 11 December
Sample
$scope.inlineDatepicker.date.getUTCDate(); //prints day of month
$scope.inlineDatepicker.date.getUTCMonth() + 1; //prints month, 0 based, so add 1
$scope.inlineDatepicker.date.getUTCFullYear(); //prints 2015
Get day returns the day of the week. Use getDate() to return the day of the month. If you do getMonth(), january is 0 and december is 11.
You can see the reference documents here: http://www.w3schools.com/jsref/jsref_obj_date.asp