Using fullCalendar and getting the wrong day after formatting [duplicate] - javascript

This question already has answers here:
Time not working as expected using moment.js
(2 answers)
Closed 6 years ago.
I am using full calendar and what should be very basic "dayClick" I am having trouble getting the date I clicked as a string.
dayClick: function(d){
var thisDay = d._d;
console.log(thisDay);
console.log(moment(thisDay).format('yyyy-mm-dd'));
console.log(thisDay.toString());
},
produces three very strange results. The 1st one I get. it is the date from the day object that I clicked on, which is correct, the second one is the result of trying to let momentJs format the date. the third is just using js toString function help out but it seems to change the date from the 4th to the 3rd.
What is going on here?

thisDay.toString() is converting it to your local time instead of showing the UTC date.
You can use thisDay.toISOString() instead.

Related

How to solve, in an elegant way and using JavaScript, the UTC problem, which subtracts a day when instantiating a Date in the format '2018-10-25'? [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 4 years ago.
I'm working on legacy system and in DB the birthday is coming this way '1992-05-18' in json. I am using AngularJS and when applying the data binding of this variable in an input type = "date", of an update form, it is necessary to instantiate a Date object. Like this:
//person.byrthday = '1992-04-26'
var person.birthday = new Date (person.birthday);
// after person.byrthday = '1992-04-25T00:00:00.000Z'
How can I solve this problem through Front End in an elegant way, without "breaking" two way data binding?
I find myself in Brasil UTC -03:00
There are a few ways to solve this problem. A quick and dirty solution could be to leverage moment.js. You can convert the response from the API to a true date format this way.
If you don't want to use an additionally library, you can make a function to parse the date string. You can do the following to parse is to become a correct date:
var dateSplit = person.birthday.split('-');
var mydate = new Date(dateSplit[0], dateSplit[1] - 1, dateSplit[2]);
person.birthday= mydate;
Take note that the month index starts at 0 (aka January=0). Hopefully this helps.

IE 11 - Date is not working [duplicate]

This question already has answers here:
ToLocaleDateString() changes in IE11
(5 answers)
Closed 4 years ago.
new Date().toLocaleDateString('en-US'); // "‎8‎/‎17‎/‎2018"
new Date("8/17/2018") //valid date
new Date(new Date().toLocaleDateString('en-US')) // Invalid Date
I am trying to create date from local date
string (see screenshot) but its not working in IE11 only. It works with normal date string though.
I know something wrong with "" double quotes but not able to get it working.
Any suggestion ?
Seems it can be done like this
new Date(new Date().toLocaleDateString('en-US').replace(/[^ -~]/g,''))
Reference Answer
just use momentjs for this.
moment("8/17/2018", "L").format() would output:
"2018-08-17T00:00:00+02:00"
(+02:00 is my local timezone. you can specify to use utc or another timezone too.)
also keep in mind L is dependent on the timezone profile you installed. this is the default en one.
you could also replace "L" with "MM/DD/YYYY"
the second argument of moment always specifies the format of your input.
it is also able to guess the input but you need to experiment with that.
.format("L") is essentially the same but in the output direction.

Date conversion is returning an old date in javascript [duplicate]

This question already has answers here:
Why do I need to multiply unix timestamps by 1000 in JavaScript?
(3 answers)
Closed 4 years ago.
I have an API which returns date values. Sample value is like 1521441000. When I formatted the date using javascript, I am getting Jan 18 1970 08:07 PM. Also I tried using Javascript Date function. Then also the same reply. Please see the console screen shot. As per the expectation I want this months(March - April 2018) date. Which I am fitering using API call. This is a third party API.I want to confirm is this the problem with API.
Why this is happening. Is this the problem with the date which is returned from the API? Or Is there any problem in my comparison?
1521441000 is time in seconds, you need to multiply that by 1000 to get miliseconds before passing to moment/date constructor:
console.log(new Date(1521441000));
console.log(new Date(1521441000 * 1000));

Getting NaN only on certain dates when converting with getTime() [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 5 years ago.
I'm working on a react app with firebase firestore for the db, the app stores a date field obtained from a datepicker in dd/mm/yyyy format as a string. I need to sort by dates and I'm converting dates to correctly sort by numbers with:
new Date(myDate).getTime()
But I'm getting NaN only on certain dates, for example:
01/12/2017 // 1484190000000
02/11/2017 // 1486782000000
22/08/2017 // NaN
Any idea???
var dateArr = '22/08/2017'.split('/');
new Date(dateArr[2], --dateArr[1], dateArr[0]).getTime();
Try this. 22/08/2017 - wrong format, need 08/22/2017

Javascript backwards date, reverse? [duplicate]

This question already has answers here:
What is the best way to convert date from JavaScript string in format YYYYMMDD to JavaScript date?
(2 answers)
Closed 9 years ago.
does anybody know How I could reverse the date that I have in javascript?
I have a created date of files from google drive which I pulled using the google drive api, however the date is reverse.
2014-02-25
I would like, 25-02-2014 (dd/mm/yyyy)
var date = resp.createdDate;
date = date.substring(0, date.length - 14)
This is my code, i trimmed the end off, because it came with time etc.
my output is currently : 2014-02-25
Im unsure how I could do this and im sure its simple!
> "2014-02-25".split('-').reverse().join('-')
"25-02-2014"

Categories

Resources