Init javascript Date object - javascript

I would like to retrieve a Javascript object Date for new year. I want to user new Date(); object and init it to the 2009-01-01 (it's for a countdown).
Thanks

The month part of the construct is an enum, so it's always themonthyouwant -1. And are you sure you want to count down to 2009? oh well...
var newYears = new Date(2009, 0, 1);

From http://www.w3schools.com/js/js_obj_date.asp, you can init your js date object with
var date= new Date(year, month, day, hours, minutes, seconds, milliseconds);
You may also use date.setFullYear(year,month,day) method if your date object has been created before. Please note that month is between 0 and 11 just like what David Hedlund said.

use...
dateVariable.setTime(Date.parse('1/1/2009 12:00 AM'));

Related

How to return a Date format when I use setDate()

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));

set date with year confusion in javascript Date()

According to w3schools :
There are 4 ways of initiating a date:
new Date()
new Date(milliseconds)
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)
so when I try console.log(new Date(2015)); it gave me 1970-01-01T00:00:02.015Z ?
It thinks that 2015 is the amount of milliseconds you want.
You could try using a calculator to see how many milliseconds the year 2015 is equivalent to, but it would be bad to maintain.
You should use one of the other ways you listed:
new Date(dateString)
new Date('01/01/2015')
or
new Date(year, month, day, hours, minutes, seconds, milliseconds)
new Date(2015,0,1)
The syntax is:
console.log(new Date(milliseconds)
The docs on MDN state clearly:
Creates a JavaScript Date instance that represents a single moment in time. Date objects are based on a time value that is the number of milliseconds since 1 January, 1970 UTC.
You initialized it with a number, which actually means milliseconds or new Date(milliseconds).
Alternatively, you can do this:
console.log(new Date('01/01/2015'));
If what you want is the current time or year, you can get it with
var now = new Date();
console.log(now.getFullYear());
Finally, I recommend you to read MDN entries which are more accurate than W3Schools'.

Add millisecond value to today's date and display Date, And Month and Year (Date, Month and Year are each separate values)

I am trying to create a testing script in Selenium and I need to enter a date. I have figured out how to get the dates using:
storeEval var d=new Date(); d.getDate() CurrentDay
store Eval var m=new Date(); (m.getMonth()+1) CurrentMonth
storeEval var y=new Date(); y.getFullYear() CurrentYear
Now I want want to create variables for times in the past and future. I have been told I can do so using milliseconds, which is amazing but the closest I can come is this:
storeEval new Date().getTime()+604800000 //604800000- being 7 days in the future
I get back: 1350932638018 which is 7 days forward according to this amazing calculator I found.
So, how do I take the number I found and extract the date, month and year as I did for today's date.
If your future date is stored in the variable d then it should be as easy as:
var n = new Date(d);
or if it isn't stored in a variable, then maybe something like this?
var n = new Date(Date().getTime()+604800000);
And then now n is a date object and you should be able to use the .getFullYear() methods.
Take a look at this fiddle and see if it helps: http://jsfiddle.net/wVVmw/
use toDateString()
So,
var newDate = (Date().getTime()+604800000).toDateString();
should return Mon Oct 22 2012
I don't know selenium, but it looks like JavaScript.

manipulating javascript date objects

I am trying to manipulate a javascript date object, to increment it by one day:
var now = new Date(+1 day);
What are the javascript options for something like this...
EDIT: cheers
Like this:
var now = new Date();
now.setDate(now.getDate() + 1);
setDate will correctly convert January 32 into February 1.
Like this:
var myDate=new Date();
myDate.setDate(myDate.getDate()+1);
setDate will increase the current date with 1, hope this will help.
Some straightforward answers have been posted, but just do you know, W3Schools has a fantastic Javascript date object reference page.

JavaScript date objects UK dates

I have the following code
datePicker.change(function(){
dateSet = datePicker.val();
dateMinimum = dateChange();
dateSetD = new Date(dateSet);
dateMinimumD = new Date(dateMinimum);
if(dateSetD<dateMinimumD){
datePicker.val(dateMinimum);
alert('You can not amend down due dates');
}
})
dateSet = "01/07/2010"
dateMinimum = "23/7/2010"
Both are UK format. When the date objects are compared dateSetD should be less than dateMinimumD but it is not. I think it is to do with the facts I am using UK dates dd/mm/yyyy. What would I need to change to get this working?
The JavaScript Date constructor doesn't parse strings in that form (whether in UK or U.S. format). See the spec for details, but you can construct the dates part by part:
new Date(year, month, day);
MomentJS might be useful for dealing with dates flexibly. (This answer previously linked to this lib, but it's not been maintained in a long time.)
This is how I ended up doing it:
var lastRunDateString ='05/04/2012'; \\5th april 2012
var lastRunDate = new Date(lastRunDateString.split('/')[2], lastRunDateString.split('/')[1] - 1, lastRunDateString.split('/')[0]);
Note the month indexing is from 0-11.
var dateString ='23/06/2015';
var splitDate = dateString.split('/');
var month = splitDate[1] - 1; //Javascript months are 0-11
var date = new Date(splitDate[2], month, splitDate[0]);
Split the date into day, month, year parts using dateSet.split('/')
Pass these parts in the right order to the Date constructor.
Yes, there is problem with the date format you are using. If you are not setting a date format the default date that is used is 'mm/dd/yy. So you should set your preferred date formate when you create it as following when you create the date picker:
$(".selector" ).datepicker({ dateFormat: 'dd/mm/yyyy' });
or you can set it later as:
$.datepicker.formatDate('dd/mm/yyyy');
When you try to create a date object:
new Date(year, month, day, hours, minutes, seconds, milliseconds)
Example:
dateSetD = new Date(dateSet.year, dateSet.month, dateSet.day);
Note: JavaScript Date object's month starts with 00, so you need to adjust your dateset accordingly.

Categories

Resources