from Date I just want to subtract 1 day in javascript/angularjs - javascript

with the help of new Date() how i can achieve this.
my code :
var temp =new Date("October 13, 2014 22:34:17");
console.log(new Date(temp-1));
requirement:
before: Aug 4, 2014 11:59pm (EDT)
after: Aug 3, 2014 11:59pm (EDT)

You need to specify what amount of time you are subtracting. At the moment its 1, but 1 what? Therefore, try getting the days using getDate() and then subtract from that and then set the date with setDate().
E.g.
var temp = new Date("October 13, 2014 22:34:17");
temp.setDate(temp.getDate()-1);

The simple answer is that you want to subtract a days worth of milliseconds from it. So something like the following
var today = new Date('October 13, 2014 22:34:17');
var yesterday = new Date(today.getTime() - (24*60*60*1000));
console.log(yesterday);
The problem with this is that this really gives you 24 hours earlier, which isn't always a day earlier due to things such as changes in Daylight Saving Time. If this is what you want, fine. If you want something more sophisticated, check out moment.js

You can simply subtract one day from today date like this:
var yesterday = new Date(new Date().setDate(new Date().getDate()-1));

Simply subtract from one date to the next Date. You can do so by clicking on this link for subtracting the date. following this url.

Related

Javascript local to UTC time conversion off by two hours

I'm trying to convert a date from local to midnight UTC so that I can then save into a SQL database and be able to query by matching dates. For example, if today is Sep 20, 2015 at any time in any time zone that I may be in, then I should end up with a timestamp of Sep 20, 2015, 0:00am UTC, but instead I end up with Sep 20, 2015, 2:00am UTC regardless of which time zone I set my clock to. This should be simple, but for some reason when I convert to UTC I end up off by two hours, so I delete 7200 seconds to get midnight. This seems to be working, but because I don't know why I'm off by two hours I'm afraid I may run into problems later. My code is below, only 4 lines. Any help would be appreciated.
var date = new Date();
date.setUTCFullYear(date.getFullYear(), date.getMonth(), date.getDate());
date.setUTCHours(0,0,0,0);
dateUTC = date.getTime()/1000-7200;
It's not clear what exactly isn't working, but you should use Date.UTC() and it will work:
var date = new Date();
alert(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate())/1000)
To get rid of time zone difference you could try:
date.setMinutes(date.getMinutes() - date.getTimezoneOffset())

Explain javascripts Date() functions

Why is it when I have
var dt = new Date(2015, 6, 1);
dt.toUTCString()
My output is Tue, 30 Jun 2015 23:00:00 GMT
And
var dt = new Date(2015, 6, 2);
dt.toUTCString()
Wed, 01 Jul 2015 23:00:00 GMT
I'm clearly missing something here, I want to be able to loop through each days of the month and get a Date() for that day
I don't understand why if the day is 1, it says the date is the 30th
Javascript dates are always generated with local time zone. Using toUTCString converts the time in the Date object to UTC time, and apparently in your case that means -1 hours. If you want to initialize a Date object with UTC time, use:
var dt = new Date(Date.UTC(2015, 6, 1));
The toUTCString() method converts a Date object to a string, according to universal time.
The Universal Coordinated Time (UTC) is the time set by the World Time Standard.
Note: UTC time is the same as GMT time.
Try to change dt.toUTCString() in another function.
There are a lot of hour on the planet,for example in America is the 5 o' Clock,in Japan is 10 o' Clock etc... the UTC is a time zone,try to change this.

manipulating javascript date object

I know with a java calendar, you can go back to a previous date, but how do I do that with a javascript date? Lets say I want to go backwards three months, how do I do that? I'm assuming that there has to be some logic to do that, and not just do a setMonth(), since rolling back 3 months may take you back to the previous year, and so the year needs to be updated too.
Why don't you use one of the date libraries such as date.js
http://datejs.com
Date.add(-3).month();
Any rollover date manipulation to a new period (ie new year, month, week, day, etc) is handled automatically
var d = new Date();
-> Tue Oct 01 2013 14:12:21 GMT+1000 (EST)
d.setMonth(d.getMonth() - 3);
-> Mon Jul 01 2013 14:13:43 GMT+1000 (EST)
d.setMonth(d.getMonth() - 10);
-> Sat Sep 01 2012 14:14:31 GMT+1000 (EST)
Using get month you can get the value from 0-11,so if you want to go back to 3 months just chekc if month no. is greater than 3 ,if it is greater than 3 no need to change the year,else decrement the year by 1
MomentJS is a JavaScript library that is great for date manipulation. Using MomentJS, you could do something like:
// Subtract 3 months from the current moment (now)
moment().subtract('months', 3);
If you must use the JavaScript Date object, you can use the setMonth() but it can be unreliable (as you mentioned). As an example:
// Get the next and previous month from now (first day of the month)
var now = new Date();
var futureMonth = now.setMonth(now.getMonth() + 1, 1);
var pastMonth = now.setMonth(now.getMonth() - 1, 1);
Note that you need to specify the second parameter to set the day to 1. This will prevent the "next" month from skipping a month (e.g. adding a month to January 31, 2014 will result in March 3rd, 2014 if you omit the second parameter).
No, you don't need to worry about roll backing the year. Just set the month and remaining it with JS rendering machine.
var date = new Date();
date.setMonth(-10); //+ increase the month or - decrease
alert(date);
JSFiddle

JavaScript Countdown Time

I'm using this plugin to do a countdown. In his example, he is counting down to Australia Day
$(function () {
var countdownDate = new Date();
countdownDate = new Date(countdownDate.getFullYear() + 1, 1 - 1, 26);
$('#countdown').countdown({until: countdownDate});
$('#year').text(countdownDate.getFullYear());
});
I need it to count down to 8 PM EST on Tuesday, November 6, 2012, but I'm not sure how to customize it. The Date() function confuses me.
The Date constructor shown above is in the format new Date(Year, Month, Day). However, keep in mind that the Month argument is 0-indexed. That means that January is 0, not 1. So to count down to November 6th, 2012, you would want to construct the date like this:
countdownDate = new Date(2012, 10, 6);
You might also consider doing:
countdownDate = new Date(2012, 11-1, 6);
Which is similar to what the example shows. But it just slows things down unnecessarily in order to be more clear about what month you're referring to.
Additionally, if you construct a Date object with no arguments, it gives you the current date. So new Date() gives you a date object equivalent to "now". In the example, they use that to get the current year using (effectively) (new Date()).getFullYear(). They then increment it by one and pass it into a new Date constructor in order to get the time until the "next" Australia Day.
It should be noted that the Australia Day example actually has a bug. If it's currently January, then the year will be unnecessarily incremented and the countdown will show the time until the following year's Australia Day. So in the example, the countdown will never drop below 26 days. Whoops. :-)
UPDATE:
Either your question got updated or I missed this the first time around. Looks like you wanted to end at 8PM EST. That's actually pretty tricky using the numerical date constructor. Because JavaScript runs on the client side and uses "local time" by default, you need to explicitly note the time zone. You can do this using the setUTC versions of setters (e.g. setUTCHours()), but it's a bit annoying and takes several lines of code. So your best bet is to use Date's String-based constructor:
new Date("November 6, 2012 20:00:00 GMT -5:00")
The Date() constuctor calls you'd want to use would be one of the following:
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)
Soy, you could do
new Date("November 6, 2012 20:00:00 GMT -5:00")
and that would do it.
countdownDate = new Date(2012, 10, 6, 20, 0, 0, 0);
or
countdownDate = new Date("11/6/2012 8:00 PM")
see https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date
These lines determine the date that it's counting down to:
var countdownDate = new Date();
countdownDate = new Date(countdownDate.getFullYear() + 1, 1 - 1, 26);
The first line here creates a new JavaScript Date Object. It defaults to right now when you create a Date.
The next line sets the date to a specific point in time using the structure:
new Date(year, month, day, hours, minutes, seconds, milliseconds);
I'll go one step at a time:
1. Year it gets the current year with countdownDate.getFullYear(), add one to make it next year
2. 1 - 1 = 0, so this is selecting the first month (January)
3. 26 is the 26th day of a particular month
To fix you can replace both of these lines with a single one using the string notation:
var countdownDate = new Date("November 6, 2012 20:00:00 EST");

The fifteenth of February isn't found

I'm in javascript, running this in the console
d = new Date();
d.setMonth(1);
d.setFullYear(2009);
d.setDate(15);
d.toString();
outputs this:
"Sun Mar 15 2009 18:05:46 GMT-0400 (EDT)"
Why would this be happening? It seems like a browser bug.
That's because when you initialize a new Date, it comes with today's date, so today is Oct 30 2008, then you set the month to February, so there is no February 30, so set first the day, then the month, and then the year:
d = new Date();
d.setDate(15);
d.setMonth(1);
d.setFullYear(2009);
But as #Jason W, says it's better to use the Date constructor:
new Date(year, month, date [, hour, minute, second, millisecond ]);
It's probably best to construct a Date object in one step to avoid the Date object being in an ambiguous or invalid state:
d = new Date(2009, 1, 15);
d = new Date();
d.setDate(15);
d.setMonth(1);
d.setFullYear(2009);
d.toString();
This works.
After a bunch of testing in FF3 on XP with Firebug, here are the things I can tell you
Calling Date.setDate() after calling Date.setMonth() will generate this odd behavior.
Date.setMonth() forces the timezone to be CST (or, some non DST-aware zone)
Date.setDate() forces the timezone to be CDT (or, some DST-aware zone)
So, there's definitely something wonky going on with setMonth() and setDate() in respect to the timezone.
The only solution I can offer is this: Set the date before you set the month.
This will work generally to avoid the rollover behavior of the javascript Date API:
d.setDate(1);
d.setFullYear(year);
d.setMonth(month);
d.setDate(day);
Given that year + month + day are in a "valid" combination, e.g. taken from another Date object using getFullYear(), getMonth(), getDate().
The important parts are:
starting with setDate(1) to avoid possible rollover when the current date value is 29, 30 or 31
call setMonth(month) before setDate(day) to avoid the same rollover in case the current month value is "problematic" (because then the initial setDate(1) would be without effect)

Categories