How can I convert GTM-0006 to ISO without Adding hours - javascript

I'm working in a from who has a date field and by default it shows the current date.
I set the date using this:
var date = new Date(); = Tue May 25 2021 17:06:01 GMT-0600 (Mountain Daylight Time) {}**
Everything works fine, but when I send the data to the controller, the JSON automatically converts it to ISO and the date received by the controller is 6 hours in advance.
I understand a little bit the context about GMT-0006 (my current timezone is 6 hours more than the 0 timezone), and the fact that my controllers received the date in ISO format because when I converted to ISO format is the same problem
date.toISOString() = "2021-05-25T23:06:01.861Z" (6 hours in advance)
so my question is, there is a way to create a date that allows me to use .toISOString() and keep the same?
or create a date with my current hour but -0000 so when I convert it to toISOString keeps the same?

Related

Moment.js - UTC date to millis since unix epoch

I am trying to get an UTC Date using the moment.js library (just to send it to my server), as follows:
const eighteenYearsAgoUTC = moment().utc().subtract(18, "years").toDate();
const eighteenYearsAgoUTCSinceUnixEpoch = eighteenYearsAgoUTC.valueOf()
console.log(eighteenYearsAgoUTCSinceUnixEpoch);
The millis since unix epoch are: 1051875596343
But... if I do the same without utc, I get the same result
const eighteenYearsAgoUTC = moment().subtract(18, "years").toDate();
const eighteenYearsAgoUTCSinceUnixEpoch = eighteenYearsAgoUTC.valueOf()
console.log(eighteenYearsAgoUTCSinceUnixEpoch);
1051875596343
Why am I getting the same milliseconds since Unix Epoch for an UTC date and a local Date?
My local date is: Fri May 02 2003 13:37:00 GMT+0200 (Central Europe)
The utc method just changes how moment parses and formats dates. The underlying information is still the same.
Milliseconds-since-The-Epoch values are always UTC. Both of your code snippets do the same thing:
Get "now"
Subtract 18 years
Get the result as the milliseconds-since-The-Epoch value
You'd notice a difference if you were formatting a date or parsing one, but you aren't doing either of those things.

How do I convert ISO date format to yyyy-MM-dd format using momentjs or vanilla JS? [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 4 years ago.
I'm trying to hit a soccer sports API which includes date in the format of yyyy-mm-dd, only the scores from that date to current date will be displayed. The current date is chosen by user using a calendar but when the user chooses the date from calendar, it gets displayed in ISO format as "Fri Aug 17 2018 00:00:00 GMT +0545 (Nepal Time)" . I want to convert this date in the front end in the yyyy-mm-dd format and send it to the API Url in back end. I'm using AngularJS and Java. How do I convert the full ISO date into that format?
Based on that output it sounds like your date is stored as a JavaScript date object (see: https://www.w3schools.com/js/js_dates.asp)
To get the string you want, one solution would be to take the value of your input (I'll call it d) and do the following (I assume you have momentjs loaded:
var datestring = moment(d).format('YYYY-MM-DD')
datestring should now include the date in the format you want... if for some reason d is a string instead of a date object, you can create a parsing pattern following the momentjs doc here: https://momentjs.com/docs/#/parsing/
Assuming you have a JavaScript Date object to work with, you can do this in plain JS:
var datestring = dateobj.toISOString().substring(0, 10); // 'yyyy-MM-dd'
If you only have the display string ("Fri Aug 17 2018 00:00:00 GMT +0545 (Nepal Time)"), you can first convert that into a Date object with this:
// displaystring = "Fri Aug 17 2018 00:00:00 GMT +0545 (Nepal Time)";
var dateobj = new Date(displaystring);
...and then do the datestring conversion above.

the date in Javascript is swapping between day and month in?

I Have date as a string 10/06/1991 when I need to convert to a date object the day and month value is swiping like this Sun Oct 06 1991 00:00:00 GMT+0200 (GMT+03:00), but if I reverse my string to 1991/06/10 it's correct after converting.
is there any idea how to solve it?
You cannot parse string to date using Date object unless it is in ISO form. Here later date - 1991/06/10 is in in correct order so js can make correct date object.
For your case, you can use any other library like moment for parse your given date 10/06/1991 by giving its format to moment, example is given below -
console.log(moment("10/06/1991", "DD/MM/YYYY").toDate());
<script src="https://momentjs.com/downloads/moment-with-locales.min.js"></script>

Javascript New Date Changing Hour Value

I am receiving times in the an AJAX request and am converting them using the new Date() function.
I receive 2013-06-18T12:00:15Z
However, somehow I get the following after new Date():
Tue Jun 18 2013 08:00:15 GMT-0400 (EDT)
Why is it not:
Tue Jun 18 2013 12:00
See the following demo:
http://www.w3schools.com/js/tryit.asp?filename=tryjs_date_convert
This is a time zone problem. You must be in the EDT timezone (GMT-0400). To correctly parse the date you should tell the parser in which timezone your date is correct.
For you parse your date like this :
new Date('2013-06-18 12:00:15 GMT-0400')
"GMT-0400" means GMT time minus 4 hours
Or if you don't wish to reformat your string, you can use the date.getUTC* functions to get the time as you parsed it.
The full list is available at Mozilla's documentation.
I agree with Vaim Caen's answer that this is a timezone issue, but not with parsing - the date is being parsed fine, but into your local timezone, while you're expecting it to be parsed into UTC date.
This answer shows how to convert from your current timezone to UTC - applying this to the TryIt demo gives:
var msec = Date.parse("2013-06-18T12:00:15Z");
// or: var msec = Date.parse("Tue Jun 18 2013 08:00:15 GMT-0400 (EDT)");
var d = new Date(msec);
d.setTime( d.getTime() + d.getTimezoneOffset()*60*1000 );
document.getElementById("demo").innerHTML = d;
Edit: If you all you're interested in is displaying the date (no further manipulations) then you can use:
d.toUTCString()
which will show the date in GMT (for me it actually shows "GMT" so most likely not of use!)
The alternative is to add a function to the prototype to show the date in whatever format you want and use the date.getUTC* methods.

How do you parse a date from an HTML5 date input?

I have an input on my webpage that I am able to set the date on by getting an ISO string and pulling out the first 10 characters.
date = new Date();
dateInput.value = date.toISOString().substr(0,10);
This works perfectly. My problem is that when I try to get the date back out. I am getting the date one day off.
var newDate = new Date(dateInput.value);
I have also tried the following code to make up for it, but it is not always correct either
new Date(Date.parse(element.value) + 86400000)
So my question is: Is there an elegant way to get the correct date consistently. I have been looking around for a little while, but it seems there is not a lot of consistency with date parsing in Javascript.
If it's an actual date input on a supporting browser, then it will have a valueAsDate property. There's no need to parse it.
It's interpreting the date as UTC, which, for most time zones, will make it seem like "yesterday". One solution could be to add the time-zone offset back into the date to "convert" it to your local timezone.
var newDate = new Date(dateInput.value + 'T00:00');
This will give the correct date in any timezone.
You can also convert the input string from YYYY-MM-DD to MM/DD/YYYY and it then parse the date to get the correct answer.
EXAMPLE:
Don't use:
new Date(Date.parse('2018-09-28')) // Thu Sep 27 2018 19:00:00 GMT-0500 (Central Daylight Time)
Rather use
new Date(Date.parse('09/28/2018')) // Fri Sep 28 2018 00:00:00 GMT-0500 (Central Daylight Time)
(NOTE: I am in CDT)
Tested in Chrome, Firefox, Safari, old Edge, and IE.
as said by Marty the problem is the difference between the representation of the timezone of the input (UTC defined by W3C) and the JS timezone (local). The solution is to getTimezoneOffset (which is in minutes) and convert everything to milliseconds:
var today = document.getElementById("myInputDate").valueAsDate;
var tomorrow = new Date(today.valueOf() + 86400000 + (today.getTimezoneOffset() * 60000));
86400000 = milliseconds of a day
today.getTimezoneOffset() * 60000 = timezoneOffset in milliseconds
Try using .toLocaleString to get the date into the input, and add the time when parsing it, like this:
date = new Date();
dateInput.value = date.toLocaleString("sv-SE", {
hour: "2-digit",
minute: "2-digit",
second: "2-digit"
});
And to get the value into a Date object use:
new Date(inputdateInput.value + "T00:00");
I wrote a little post about converting between Date objects and date inputs here

Categories