JavaScript Changes My Date Based on Browser Time Zone - javascript

I am trying to create a Date object in JavaScript, passing a string like this:
2014-11-30T00:00:00.0000000
However, the value of the Date object is:
Sat Nov 29 2014 17:00:00 GMT-0700 (Mountain Standard Time)
It changed it to 11/29 when I want 11/30. Is there any way I can make the date 2014-11-30, regardless of what time zone the browser is in?
Note: One possible workaround is to use the Date(year, month, day) constructor; however, I am constructing the data in a JSON string, which doesn't appear to support this.
EDIT:
Actually, I just did a test and created a date using Date(2015, 1, 1) and it gives me:
Mon Feb 02 2015 00:00:00 GMT-0700 (Mountain Standard Time)
So I can't even create a date that way and have it be the date I want. I don't understand why this is so difficult.

You can use Date.UTC
The UTC() method differs from the Date constructor in two ways.
Date.UTC() uses universal time instead of the local time.
Date.UTC() returns a time value as a number instead of creating a Date
object.
EDIT - why does SO insist on making links so hard to spot? That, up there, is a link to the docs in case that wasn't obvious.
EDIT 2 - I think I misunderstood. Try this:
var d = new Date('2014-11-30T00:00:00.0000000');
var utc = new Date(
d.getUTCFullYear(),
d.getUTCMonth(),
d.getUTCDate(),
d.getUTCHours(),
d.getUTCMinutes(),
d.getUTCSeconds()
);
alert('d: ' + d + "\n" + 'utc: ' + utc);

Related

javascript timestamp doesn't works in FireFox and in IE [duplicate]

I have an existing date time string in place
new Date('2014-08-01T00:00:00')
But instead of returning 2014-08-01, it returns as 2014-07-31 in the actually angularJS view.
I wonder is this date time string valid, if not, why its not valid.
Could the T be the reason that the string return a wrong date?
The console.log return a date of Thu Jul 31 2014 20:00:00 GMT-0400 (EDT)
Thank You
Lets call those -2 are toxic vote downs. They should really recall the days when they are struggling to understand the basic concepts that now apparant to them. Its a shame.
At present (Autumn 2014), JavaScript's date/time format diverges from ISO-8601 in a very important way: If there's no timezone indicator on the string, it assumes Z ("Zulu", GMT).
So
new Date('2014-08-01T00:00:00')
...is August 1st at midnight GMT. If you live east of GMT, that will be on the 31st in your local time.
However, this incompatibility with ISO-8601 is being fixed in ES6 and some implementations (including the latest V8 in Chrome) are already updating it. The ES6 spec changes the default to local time; check out ยง20.3.1.15 ("Date Time String Format", the section number may change) in the draft PDFs or this unofficial HTML version.
The displayed date uses the timezone of your browser/computer. This means that if you are in GMT-1 and you enter 2014-08-01T00:00:00, the actual date is 2014-08-01T00:00:00 - 1 hour = 2014-07-31T23:00:00
I have this date in startdate=2021-10-27T00:00:00-04:00,
d=new Date(data.StartDate) // outputTue Oct 26 2021 23:00:00 GMT-0500
But date is getting one day before'Tue Oct 26 2021 23:00:00 GMT-0500' in central timezone(below -6,-7,-8...).
Actually I used this it is working fine but for central timezone not working
var d = new Date(data.StartDate);
console.log(data.startDate);
$scope.txtStartDate = ("0" + (d.getMonth() + 1)).slice(-2) + "/" + ("0" + d.getDate()).slice(-2) + "/" + d.getFullYear();

How to take only part of the javascript timestamp

I have a js timestamp of Tue Sep 30 2014 12:02:50 GMT-0400 (EDT)
with .getTime() I got 1412092970.768
for most cases, its a today's specific time stamp. I wonder, if I could always ONLY pick out the day month and year and hour, min, day will be always stay with 0.
So for our situation, it should become Tue Sep 30 2014 00:00:00 GMT-0400 (EDT).
I wonder what kind of conversion should I be doing? Because seem convert to unix timestamp with getTime() will result in unknown way of calculation... and I can not really find a way to set time like I would do in PHP.
Any fix for this situation?
Thanks
You can create a date object and then zero-out any components you don't need, or create one with the components you specified, e.g.
foo = new Date();
foo.setHour(0);
foo.setMinute(0);
or something more like
foo = new Date(); // "now"
bar = new Date(foo.getYear(), foo.getMonth(), foo.getDate(), 0 , 0, 0, 0);
// create new date with just year/month/day value, and time zeroed-out.
The constructor's args are detailed here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
An other option is to send epoch to PHP:
JS:
long epoch = System.currentTimeMillis()/1000;
PHP:
$dt = new DateTime("#$epoch");
$dt->format('Y'); //year

JavaScript creating new Date from a string without changing timezone

I'm trying to convert a date string into a date object without changing the timezone. Here is the standard behavior:
new Date ("2014-10-24T00:00:00")
result
Thu Oct 23 2014 19:00:00 GMT-0500 (Central Daylight Time)
I am able to reverse the timezone by getting the offset in minutes, multiplying it by 60,000, and then adding that to the new string date.
new Date(new Date("2014-10-24T00:00:00").getTime() + new Date().getTimezoneOffset()*60000)
This works, but it seems like there must be a better way that doesn't require created three date objects.
Do not parse strings using the Date constructor. It calls Date.parse which, despite being standardised for one version of ISO 8601 strings in ES5, is still almost entirely implementation dependent.
I'm trying to convert a date string into a date object without changing the timezone.
> new Date ("2014-10-24T00:00:00")
That string will be treated differently in different browsers. If you want it to be treated as UTC, then it is simple to parse yourself:
function parseISOAsUTC(s) {
var b = s.split(/\D/);
return new Date(Date.UTC(b[0],--b[1],b[2],b[3],b[4],b[5],(b[6]||0)));
}
console.log(parseISOAsUTC('2014-10-24T00:00:00').toISOString()); // 2014-10-24T00:00:00.000Z
Now you can be certain that the string will be treated as UTC in all browsers in use (including the 20% or so still using IE 8 and lower).
If, on the other hand, you want the string to be treated as a local time, then just remove the Date.UTC part:
function parseISOAsLocal(s) {
var b = s.split(/\D/);
return new Date(b[0],--b[1],b[2],b[3],b[4],b[5],(b[6]||0));
}
console.log(parseISOAsLocal('2014-10-24T00:00:00')); // Fri 24 Oct 2014 00:00:00 <local timezone>
Here is an implementation of zerkms's solution.
new Date("2014-10-24T00:00:00".replace('T', ' '))
result
Fri Oct 24 2014 00:00:00 GMT-0500 (Central Daylight Time)

GMT-0400 (EDT) Showing up after I print date in javascript

When I print out a date in javascript it adds GMT-0400 (EDT) to the end of it, is there a way I can cut this off? I'm using
date=Date()
document.write(date)
To get the date and time but I dont want the trailing GMT-0400 (EDT)
You should be able to just get the appropriate parts out of the date object:
var date = new Date();
[date.toDateString(), date.toLocaleTimeString()].join(' ');
// "Wed Oct 03 2012 12:13:56"
Use var date = new Date(); Date is a constructor and should be used with the new keyword.
You need to format it. The display, by default, is that full string.
You can use the Date API to build a string, or one of the various date formatting libraries, or even just manipulate it like a string (since it is):
var date = new Date();
document.write(date.split('-')[0]);
Also, you really shouldn't use document.write for a bunch of reasons, but I digress since your question was not about that.

JavaScript convert date to format

I'm trying to truncate a JavaScript Date object string from:
Wed Aug 01 2012 06:00:00 GMT-0700 (Pacific Daylight Time)
to
Wed Aug 01 2012
(I'm not particular, could be of format MM/DD/YYYY for example, as long as I get rid of the time/timezone)
Essentially I just want to get rid of the time and the timezone because I need to do a === comparison with another date (that doesn't include the time or timezone)
I've tried using this http://www.mattkruse.com/javascript/date/index.html but it was to no avail. Does anyone know how I can format the existing string such as to get rid of the time? I would prefer to stay away from substring functions, but if that's the only option then I guess I'll have to settle.
Edit: I'm open to options that will compare two date objects/strings and return true if the date is the same and the time is different.
The only way to get a specific format of date across different browsers is to create it yourself. The Date methods in ECMAScript are all implementation dependent.
If you have a date object, then:
// For format Wed Aug 01 2012
function formatDate(obj) {
var days = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
var months = ['Jan','Feb','Mar','Apr','May','Jun',
'Jul','Aug','Sep','Oct','Nov','Dec'];
return days[obj.getDay()] + ' ' + months[obj.getMonth()] +
' ' + obj.getDate() + ' ' + obj.getFullYear();
}
Though a more widely used format is Wed 01 Aug 2012
Use the Date object's toDateString() method instead of its toString() method.
SIDE BY SIDE DEMO
Even so, it might be better to compare the two date objects directly:
Compare two dates with JavaScript

Categories

Resources