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.
Related
This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 2 years ago.
within my Angular app : i'm receiving this format date from webservice :
myDate = "2020-03-05T08:00:00"
-> for me it's the fifth march 2020
-> for chrome , firefox , IE it's alse the same format yyyy-mm-ddThh:mm:ss
But for Safari , it seems to confuse it with :
yyyy-dd-mmThh:mm:ss
-> and it reads it as it's the 3rd My 2020
****Am i alright ?****
My purpose is to get the time and i'm using to do new Date(myDate)
should i do it differently with Safari ?
Either you can use the default Javascript Date Functionality like
const newDate = new Date(myDate)
fetch the date, day , month and create your date
Or you can use moment.js libaray to directly format your newDate object, in whatever format you like
Or if you are using specifically angular, then for rendering you can use the date pipe of angular and format it accordingly.
This question already has answers here:
Get the given date format (the string specifying the format) in javascript or momentjs
(2 answers)
Closed 3 years ago.
I have the following date-time string 25/10/2020 10:12:55 AM. Is there any available method that I can use to get it's format?
According docs https://momentjs.com/docs/#/parsing/creation-data/ you can use the following
moment().creationData().format
More about this feature request here https://github.com/moment/moment/issues/4595
In order to accomplish this you can add the parseFormat plugin using the following command: npm install moment-parseformat. Using this you should be able to do the following:
var format = moment.parseFormat('25/10/2020 10:12:55 AM');
format should have the date format string that you are looking for. Check out this link(https://momentjs.com/docs/#/plugins/parseformat/) for more information. I hope this helps!
You can get the format from creationData as long as you pass it something in a valid RFC2822 or ISO format:
moment('2020-10-25T10:12:55').creationData().format returns YYYY-MM-DDTHH:mm:ss
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.
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.
This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
How can I convert datetime microformat to local time in javascript?
Im writing up an ajax application where i have to interpret this date "2009-09-16T11:10:00" and output another string to something more readable.
That's the ISO 8601 date format. There's an example here. If that doesn't suit your needs then a quick google search should help.
No, there isn't a built-in function for doing that. You'd have to parse it yourself. Maybe something like this:
var s = "2009-09-16T11:10:00";
var tokens = s.split(/[\-T:]/);
var date = new Date(tokens[0], tokens[1] - 1, tokens[2],
tokens[3], tokens[4], tokens[5], 0);
Then access the date string with:
alert(date.toString());
Try this js library:
http://www.datejs.com
Pretty good and recognizes different date formats. You can also test your date right on the front page.