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.
Related
I try to set a date to midnight to simplify my date manipulation, for this I wrote this part of code:
var now = new Date();
today = now.setHours(0,0,0,0);
console.log(now, today);
I'm surprised to see now contains a Date object and today a timestamp. This brings errors when I want to use getMonth() or other date's functions. It's paintful to recreate a Date object with the timestamp.
Is it normal? How can I fix this?
(Feel free to update my post to correct my bad english :)
Is it normal?
Yes
How can I fix this?
You are assigning the return value of now.setHours(0,0,0,0)to today.
Maybe what you are looking for is something like this:
var now = new Date();
var today = new Date();
today.setHours(0,0,0,0);
In this way, setHours is acting upon the value you wish to have the hours set on. This is the primary manner of using setHours.
Other details
The specification doesn't appear to mention the return value. Other sites such as w3schools do.
The Chromium setHours source shows a value being return though other functions that perform similarly do not return this value. I presume that the SET_LOCAL_DATE_VALUE function found in chromium's date.js is assigning the value into the first argument.
I had a similar situation and pcnate answer didn't solved my issue...
What I did was:
var today = new Date();
today = new Date(today.setHours(0,0,0,0));
console.log('Date: '+today);
You can manipulate dates easily using datejs or momentjs
date.js:
Date.today().set({ hour : 0 });
moment.js
moment().set({ "hour": 0, "minute" : 0, "second": 0});
i have a date in string like this: var myDateStr='1431451872338.00';
i want, getMonth() from this format date, i do:var date = new Date(myDateStr); but always return invalid date.
and the method getMont() always return NaN, if I put this: var date = new Date(1431451872338.00); this return the date correct but with my string not
my var myDateStr get the value from json and is variable, if someone can help me thank you very much in advance, i hope do understand
This works fine for me. You just need to be sure you're inputing a number, not a string.
var number = parseInt("1431451872338.00");
var date = new Date(number); //Tue May 12 2015 12:31:12 GMT-0500 (CDT)
var month = date.getMonth(); // 4
A Date object cannot be instantiated with a string. You better 1st transform your string into an Int and then ask for month:
var myDateStr='1431451872338.00';
var date = new Date(parseInt(myDateStr, 10));
alert(date.getMonth());
Could you use parseInt and do something like this:
var myDateStr = '1431451872338.00';
var myDateInt = parseInt(myDateStr, 10);
var myDate = new Date(myDateInt);
When you're passing in a date string to the javascript date object, it needs to be in the format "yyyy/mm/dd" or something like "January 10, 2014". What you're passing is the number of milliseconds since January 1, 1970, which is only accepted by the date object as a number. You need to change the type of your input variable.
Please make sure to research carefully before answering questions - the answer to your question is clearly stated on many date references like this one.
One Liner
var month = date.getMonth(Date(parseInt("1431451872338.00")));
I have a date in this string format "02/28/2012" and I want to convert it to UTC.
I'm using the jquery datepicker to select thedate and populate an inputbox. any clues?
Thanks
var datestr = "07/08/2005";
var datearr = datestr.split("/")
var utc = Date.UTC(datearr[2],datearr[0],datearr[1]);
var utcdate = Date.UTC(2012,2,28);
The other answers are good, but they will give you the wrong result.
In Javascript, the month argument is zero-indexed, so make sure to subtract 1 from the standard month number,
var utcms = Date.UTC(2012,2-1,28);
Unfortunately jquery .datepicker.parseDate(str) injects a local timezone (it would be nice if the documentation said this), and Date(str) and Date.parse(str) appear unpredictable about their treatment of local vs UTC.
Hey, just wondering how to convert an HH:MM string into a javascript Date object. I have tried new Date(string); and myDate.setTime() but to no avail.
A side question could be: How to convert a string in HH:MM into milliseconds from Jan 1, 1970.
Thanks for your help in advance.
How about something like:
//using timestr '10:33:21', could also be '10-33-21'
var dat = new Date, time = timestr.split(/\:|\-/g);
dat.setHours(time[0]);
dat.setMinutes(time[1]);
in JavaScript, I'm using the datejs library. http://www.datejs.com/
If you include this library, you have a function called "parseExact" and you could use it like this:
var dateString = "10-12";
var date = new Date.parseExact(dateString, "hh-mm");
To get the miliseconds, you can download the file time.js from http://code.google.com/p/datejs/source/browse/trunk/#trunk/src. Then you have a function getTotalMilliseconds() you can use:
var mSeconds = date.getTotalMilliseconds();
I hope this will help a little bit.
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'));