My js code is as follows:-
var earlierdate=new Date(2012,09,22);
alert(earlierdate.getDay());
var date2 = new Date('2012,09,22');
alert(date2.getDay());
The problem is first alert gives me 1 0r Monday(which is incorrect) and second one gives me 6 or sat which is correct. So date given in quotes is giving correct result. Now if want to use variables instead of hard-coded values like
var date1 = new Date(a,b,c);
alert(date1.getDay());
What should be the syntax. I have tried a lot of variations but failed.
Thanks in advance.
The month parameter of Date is 0-indexed.
month
Integer value representing the month, beginning with 0 for January to
11 for December.
So if you mean September, that should be var earlierdate=new Date(2012, 8, 22);
//Option 1
var myDate=new Date();
myDate.setFullYear(2010,0,14);
//Option 2 (Neater)
var myDate=new Date(2010,0,14);
This will set the time to 14th January 2010.
Related
I assigned the weekEnd as the current end of the week like this
this.weekEnd = new Date(this.currentDate.setDate(end));
Next, what I want is to give a new value to the weekEnd which is the weekEnd + 7 days. I've done it like below but I can't assign the new value to the weekEnd because the right side returns a Number and not date.
this.weekEnd = this.weekEnd.setDate(this.weekEnd.getDate() + 7);
If you guys have any idea how I can do it I would appreciate it. Thanks a lot!
Just use the .setDate() method without the assignment:
this.weekEnd.setDate(this.weekEnd.getDate() + 7);
.setDate() does two things:
It changes the current Date object to the new date.
Returns the number of milliseconds of that new date since 1 Jan 1970.
In your code you assigned this number to the variable that would have held the correct date anyway.
Try this. It will return 15th Jun and 21st June. If you remove +1 then it will start from sunday. To start from monday you have to add 1.
let current = new Date;
let firstday = new Date(current.setDate(current.getDate() - current.getDay()+1));
let lastday = new Date(current.setDate(current.getDate() - current.getDay()+7));
I am using Moment.js for adding dates to an option list I am making, so that I can use those dates to show available appointments, for example, someone can select Friday, February 3rd from the option list and then a list of available times will show up for February 3rd.
What I am having trouble with is I am using an online scheduling api that takes month values that start at 1, for example, January is 01, February is 02, etc. but moment.js months start at 0 and only go up to 11, for example, January is 0, February is 1, etc. I need to be able to convert the values from moment.js into an integer so I can add 1 to each to account for the difference.
The real problem is I tried using parseInt(Month) to get the int value to add one to it, but that didn't work. Here is my code attempting to do just that:
var d = moment(),
Month = d.month();
var GetMonthint = parseInt(Month),
GetMonth = GetMonthint++;
GetAppointmentDates(GetMonth, Year);
GetMonth still only returns 1 for February, is there some special way I can get the int value from the month?
The line
GetMonth = GetMonthint++;
is the problem. The ++ operator returns the original value not the incremented value. You should do this:
GetMonth = GetMonthint + 1;
So I do not know if I doing something extremely wrong and stupid but here is my problem. Basically I am trying to learn the weekday value of the 1st day of the current month. To do this I created a Date object with current year and month but 1 as the date. But here is an interesting situation
var x = new Date()
console.log(x.getYear())
this prints out 115 to the console. I guess that is how they decided to represent 2000s. So if I do
var x = new Date();
var thismonth = new Date( x.getYear(), x.getMonth(), 1)
console.log(thismonth.getDay())
This prints out 5 when it should be printing out 0.Since 1st of Feb was a Sunday. On the other hand
var x = new Date(2015, 1 ,1 )
console.log(x.getDay())
prints out 0. So JS represents 2015, 115 but then can not handle its own representation ?
Is this caused by a problem in my browser or is this the general situation >
Thanks
You should use getFullYear() instead of getYear(). That will return what you are expecting.
Check out the API for more info on what each of the methods actually do.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
Notice that the description of getYear() is: "Returns the year (usually 2-3 digits) in the specified date according to local time. Use getFullYear() instead." (emphasis added)
I am trying to use gettime to sort my date string. But it is returning some vague values like.
1428303000000 16/06/2014 16:50
1389074040000 01/07/2014 16:54
The first date is smaller than second so its no. of milliseconds should also be smaller.
You can also check it on http://www.tutorialspoint.com/cgi-binpractice.cgi?file=javascript_178
So don't know why this is behaving like this.
Any help ?
Test your code if you are correctly creating Date object
// new Date(year, month, day, hour, minute, second, millisecond);
// Working with date 16/06/2014 16:50
var foo = new Date(2014, 6, 16, 16, 50);
foo.getTime(); // 1405518600000
// Working with date 01/07/2014 16:54
var foo = new Date(2014, 7, 1, 16, 54);
foo.getTime(); // 1406901240000
Read more about Date object reference.
Until we see your code and how do you get from "16/06/2014 16:50" to "1428303000000", I can't help more.
You're probably creating the date using 16/06/2014 and intending this to mean the 16th day of the 6th month. However, this is not how it's parsed. The first element is treated as the month; the second element is the day. Since there aren't 16 months in a year, the date is rounded forward to the next year (i.e. the 16th month of 2014 is the 4th month of 2015).
In other words:
Date.parse("16/06/2014 16:50") === Date.parse("04/06/2015 16:50"); // => true
I'm developing with node.js, and I'm trying to create a date object on the server.
when coding:
var birthyear = 2000;
var birthmonth = 7;
var birthday = 24;
var date = new Date(birthyear, birthmonth, birthday);
console.log(date);
OUTPUT:
Thu Aug 24 2000 00:00:00 GMT+0300 (Jerusalem Daylight Time)
As you can see, I'm getting August instead of July.
How can I fix that issue ?
The month argument in the Date() constructor doesn't start at 1 for January, but instead at 0. Therefore, supplying the month value of 7 gives you the eight month, which is August.
From MDN:
month: Integer value representing the month, beginning with 0 for January to 11 for December.
Months in JS start at 0
so it's a quite an easy fix:
var date = new Date(birthyear, birthmonth-1, birthday);
DEMO
Months in the JavaScript Date() constructor are 0-indexed, meaning that month 7 actually is August. Use the value 6 for July.
Annoyingly enough, Date is rather inconsistent about which fields are 0-indexed and which are 1-indexed, and in fact the same field can be either one depending on the context. You should always refer to documentation when you have to use dates.
Yea, that's weird, but months should be counted starting with
0