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();
Related
I'm trying to change the date format in javascript where in a variable i m getting the date value like this "11/09/2019" and want to change in a format like this "11-09-2019".but somehow i'm getting this format "sat nov 09 2019" whereas it should be "tue sep 11 2019".
can anyone help me. any help would be appreciated.
var alt_date1 = "11/09/2019";
var date = new Date(alt_date1);
alert(date);
output: Sat Nov 09 2019 00:00:00 GMT+0530 (India Standard Time)
If I understand your question right, you want a different format i.e., "-" instead of "/" and you're also getting the wrong date? 2 days ahead? Depending on your environment (browser, node, etc) the Date API can behave differently. I think this link might help you: JavaScript's getDate returns wrong date (Not sure though, some answers pertain to the time zones it uses)
As for formatting look here:
https://codehandbook.org/javascript-date-format/
Example from that site:
let current_datetime = new Date()
let formatted_date = current_datetime.getDate() + "-" + (current_datetime.getMonth() + 1) + "-" + current_datetime.getFullYear()
console.log(formatted_date)
// output
"19-10-2018"
It's a little bit tricky to understand the question fully, but if you don't need a Date object, and you want to just format the string as 11-09-2019 it can be achieved with the simple replace function like this:
alt_date1.replace(/\//g, '-')
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);
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
I'm relatively new to javascript, so this may be a really simple question. Is there an easy way to stop 'EDT' from printing after a date returned with toLocaleString?
Thanks!
There is no way to be certain what toLocaleString will return; you certainly couldn't guarantee EDT would show up on every machine that runs it, let alone any indication of timezone.
From Mozilla's developer network:
The toLocaleString method relies on the underlying operating system in formatting dates. It converts the date to a string using the formatting convention of the operating system where the script is running. For example, in the United States, the month appears before the date (04/15/98), whereas in Germany the date appears before the month (15.04.98). If the operating system is not year-2000 compliant and does not use the full year for years before 1900 or over 2000, toLocaleString returns a string that is not year-2000 compliant. toLocaleString behaves similarly to toString when converting a year that the operating system does not properly format.
One possible workaround would be to construct a custom date string using toLocaleDateString and toLocaleTimeString.
// Something to this effect:
var d = new Date();
console.log(d.toLocaleDateString() + " " + d.toLocaleTimeString());
This generally wouldn't include the time zone in its output, but even this isn't perfect as you can't know what the exact format of the output would be.
Thus, the best solution would be to use a custom date-formatting function:
// Add leading-zeros to numbers less than 10[000...]
function padZ(num, n) {
n = n || 1; // Default assume 10^1
return num < Math.pow(10, n) ? "0" + num : num;
}
function formattedDate(d) {
var day = d.getDate();
var month = d.getMonth() + 1; // Note the `+ 1` -- months start at zero.
var year = d.getFullYear();
var hour = d.getHours();
var min = d.getMinutes();
var sec = d.getSeconds();
return month+"/"+day+"/"+year+" "+hour+":"+padZ(min)+":"+padZ(sec);
}
For an in-depth look at the available Date methods, check out Date on the MDN.
As far as I can tell, no browser returns 'EDT' from toLocaleString, on windows anyway,
and only Chrome returns the timezone at all.
Other platforms may assign the string differently.
My bigger beef is that Chrome uses a 24 hour clock for local time.
// testing new Date().toLocaleString() (windows 7)
Safari 5.0>> Tuesday, June 14, 2011 15:13:43
Chrome 9.0.597.98>> Tue Jun 14 2011 15:15:01 GMT-0400 (Eastern Daylight Time)
Opera 11.01>> 6/14/2011 3:15:37 PM
Firefox 4.0.1>> Tuesday, June 14, 2011 3:16:33 PM
MSIE 8.0>> Tuesday, June 14, 2011 3:16:06 PM
MSIE 9.0>> Tuesday, June 14, 2011 3:17:09 PM
They all return the hours:minutes:seconds in a group,
so to exclude anything after the time you could:
var d=new Date().toLocaleString();
var s= d.toLocaleString().match(/^[^:]+(:\d\d){2} *(am|pm)\b/i)[0];
returned value: (Chrome)
Tue Jun 14 2011 15:26:11:11
Another way is to concat the locale day and time strings, which, surprisingly, does not return the timezone on chrome- but your milage may vary.
var D=new Date();
D.toLocaleDateString()+' '+D.toLocaleTimeString()
returns Tuesday, June 14, 2011 15:44:35 in Chrome
var dateWithoutTimeZone = function() {
return new Date().toLocaleString().replace(/\s*\(?EDT\)?/, '');
};
dateWithoutTimeZone(); // => "Tue Jun 14 2011 2:58:04 GMT-0400"
Is the time zone always at the end of the string? (The time zone doesn't appear at all when I try.)
If it is, you can use the slice method to remove the last four characters of the string (space + 3-character time zone).
document.write(mydate.toLocaleString().slice(0,-4));
I'm using the following piece of code:
$.log('Ending time: ' + ending_time);
$.log('Now: ' + new Date());
$.log('Difference: ' + new Date(ending_time - new Date()));
The output is the following:
Ending time: Thu Apr 23 2009 14:31:29 GMT+0200
Now: Thu Apr 23 2009 11:56:02 GMT+0200
Difference: Thu Jan 01 1970 03:35:26 GMT+0100
I'm using the "difference" to display how many hours and minutes there are left until ending_time, but because of the timezone differences, I get the wrong time (offset by one hour.) So is there any neat way of calculating the difference taking timezones into account?
You are no longer dealing with a date, so don't convert it to one. You have a time difference, which doesn't have a time zone for instance. The result should be in milliseconds, so perform the appropriate math to get minutes, hours, days, or possibly all of the above as needed.
You should be able to use the getTimezoneOffset function.
Check it out here.
You can use the following:
(new Date()).getTimezoneOffset()
which will give you the timezone offset of the client's browser in minutes. Of course you also need to know the timezone offset of ending time.