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

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>

Related

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

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?

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.

Parse Date with Day and GMT to timestamp without timezone

I am using bootstrap date range picker and I am getting the input date from
var startDate = $('#reportrange').data('daterangepicker').startDate._d;
and the output will look like
"Sun Mar 10 2012 05:50:34 GMT +0600"
But I want the output as timestamp without timezone which is
2012-03-10 05:50:34
So I tried
var s = startDate.format('YYYY-MM-DD HH:mm:ss');
But it didn't give the result.I do not want this as a string and as a timestamp only.Any help is appreciated.
You cannot use format() on a string and expect it to format into date. Either you will have to write your own code to parse the string and convert into date and then into particular date format. Or you can use a library like moment.js.
This question has been answered in detail here How to format a JavaScript date

Javascript date formatting to local timezone with specific format

I have a date string which is in UTC timezone like this 2015-05-26T14:02:46.000Z
I will convert this to a date object using
var lastModified = "2015-05-26T14:02:46.000Z;
var date = new Date(lastModified);
But when I print this, it prints the date like this Tue May 26 2015 19:32:46 GMT+0530 (India Standard Time)
I want to print the date like this 2015-05-26 7:32:46 PM IST or 2015-05-26 7:32:46 PM GMT+0530
Is there any way to do this?
As hindmost says, it's pretty simple in pure JS. But if you'll be doing this a lot, you may want to look into the powerful MomentJS library.
Using Moment, your code would just be:
moment(date).format('YYYY-MM-DD HH:mm:ss A [GMT]Z')
Date has a number of getter method to get the components of a date. For example date.getHours() will return a number from 0 to 23 for the hour of the day. With each of these pieces you can write the logic to display the date in the format you choose. See the methods Date has here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
If you're looking for a library, MomentJS is the best library I've used for time and dates. It might be too heavy for your needs though. Consider use cases carefully before adding another somewhat large dependency.

Any way to parse a time string using Moment.js but ignore timezone info?

Given the volume of Timezone questions, I would have thought to be able to find the answer to this issue, but haven't had any success.
Is there a way using moment.js to parse an ISO-8601 string but have it parsed in my local timzeone? Essentially I want to ignore the timezone information that is supplied in the ISO string.
For example, if I am in EDT timezone:
var x = moment( "2012-12-31T00:00:00+0000" );
will give me:
"2012-12-30T19:00:00-5000"
I'm looking to ignore the timezone info and just have it give me a moment equivalent of "2012-12-31T00:00:00-5000" local time (EDT).
I don't think you really want to ignore the offset. That would ultimately just be replacing the offset you provided with one from your local time zone - and that would result in a completely different moment in time.
Perhaps you are just looking for a way to have a moment retain the time zone it was given? If so, then use the moment.parseZone function. For example:
var m = moment.parseZone("2012-12-31T00:00:00+0000");
var s = m.format(); // "2012-12-31T00:00:00+00:00"
You could also achieve this with moment.utc. The difference is that moment.parseZone will retain whatever offset you give it, while moment.utc will adjust to UTC if you give it a non-zero offset.
I solved this by supplying a format as the second argument, and using Moment's method of escaping characters, and wrapped square brackets around the timezone.
moment("2016-01-01T05:00:00-05:00", "YYYY-MM-DDTHH:mm:ss[Z]").startOf("hour").format()
This will still create moment objects using your local time zone, but it won't do any sort of auto-timezone calculation. So the above example will give you 5am regardless of timezone supplied.
I know I'm late to the party, I had the same question and my searches didn't bring me any closer. I broke down and read the documentation and there is an option in moment for a String + Format:
String + Format docs
moment(String, String);
moment(String, String, String);
moment(String, String, Boolean);
moment(String, String, String, Boolean);
and more words, then this:
Unless you specify a time zone offset, parsing a string will create a date in the current time zone.
moment("2010-10-20 4:30", "YYYY-MM-DD HH:mm"); // parsed as 4:30 local time
moment("2010-10-20 4:30 +0000", "YYYY-MM-DD HH:mm Z"); // parsed as 4:30 UTC
The part that gave me pause was the example that was used to parse local time omitted the +0000, which lead me to think the input string needed to have that removed, but it doesn't.
example:
var time = "2012-12-31T00:00:00+0000";
var x = moment(time); // Sun Dec 30 2012 19:00:00 GMT-0500
var y = moment(time,'YYYY-MM-DD'); //Mon Dec 31 2012 00:00:00 GMT-0500
You can ignore the browser's timezone completely by creating a new moment using moment.utc() instead of moment().
For example, if you are trying to work purely with a UTC date/time of the browser's current time but want to discard its timezone data, you can recreate the browser's current time into a UTC format using the following:
let nowWithTimezone = moment();
let nowInUtc = moment.utc(nowWithTimezone.format('MM/DD/YYYY HH:mm'), 'MM/DD/YYYY HH:mm');
Further documentation on moment.utc(): https://momentjs.com/docs/#/parsing/utc/
If you know for sure your input string is in the ISO-8601 format, you could just strip off the last 5 digits and use that in the Moment constructor.
var input = "2012-12-31T00:00:00+0000"
input = input.substring(0, input.length-5)
moment(input).toString()
> "Mon Dec 31 2012 00:00:00 GMT-0600"
There are valid reasons to do what the OP is asking for. The easiest way to do this with Moment is using its parseZone(date) method. No futzing around with string manipulation or multiple calls. It effectively parses the date string as though it were in the browser's local time zone.
This is difficult task to do with MomentJS, it will basically depend as well on your current timezone.
Documentation as well is vague for this specific task, the way I solved the issue on my side was by adding hours to the date before converting it to JSON format.
var dt = moment("Sun Sep 13 2015 00:00:00 GMT-0400", "ddd MMM DD YYYY HH:mm:ss GMT-0400", false);
var date = dt.add(2, 'hour').toJSON();
console.log(date); //2015-09-13T00:00:00.000Z
Momentjs default logic will format the given time with local timezone. To format original date, I wrote a function:
https://github.com/moment/moment/issues/2788#issuecomment-321950638
Use moment.parseZone to convert without taking into account the timezone.
const moment = require('moment')
const dateStr = '2020-07-21T10:00:00-09'
const date = moment.parseZone(dateStr)
console.log(date.format('MM-DD-YY HH:mm A')) // 07-21-20 10:00 AM
Try here link to docs
The best way is to use:
dt = moment("Wed Sep 16 2015 18:31:00 GMT-0400", "ddd MMM DD YYYY HH:mm:ss GMT-0400",true);
And to display convert again to desired timezone:
dt.utcOffset("-04:00").toString()
output > Wed Sep 16 2015 18:31:00 GMT-0400

Categories