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.
Related
This question already has answers here:
Format a date string in javascript
(7 answers)
Closed 2 years ago.
How do I format the date I receive from openweather api?
I currently get 2020-10-16 00:00:00 and I want 10-16-2020.
The reason I don't use moment is because I want future dates which come automatically with the 5 day forecast in the api.
You can use JavaScript's Date object.
You might save yourself time by searching a bit more before posting a question, the answer is probably already out there.
Where can I find documentation on formatting a date in JavaScript?
How to format a JavaScript date
Format JavaScript date as yyyy-mm-dd
You could try to:
Make a new date based on the date that comes from OpenWeather api
Convert it to a LocaleDateString using the 'en-US' locale. This will make the month appear before the date.
Then just split the Date String on the '/' and join in on a '-'. This will substitute the '/' with a '-'
const date = new Date("2020-10-16 00:00:00").toLocaleDateString('en-US');
const formatedDate = date.split('/').join('-');
console.log(formatedDate);
You can always use the built in Javascript Date object.
In your case you'd want to. do something like this -
const myDate = new Date('2020-10-16 00:00:00');
const date = myDate.getDate();
const month = myDate.getMonth() + 1;
const year = myDate.getFullYear();
console.log('MM-DD-YYYY', `${month}-${date}-${year}`);
If you want something more convinient and don't want to use moment you can also try some other popular date libraries. You can find some here - https://github.com/you-dont-need/You-Dont-Need-Momentjs
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.
This question already has answers here:
Parse DateTime string in JavaScript
(9 answers)
Why does Date.parse give incorrect results?
(11 answers)
Closed 5 years ago.
I am using angular JS, my Server returning a date like below.
When I tried to convert it to Date format it is saying "Invalid Date".
Please any suggestions?
Below is the sample of server date.
function runSettlement() {
return Restangular.one('/api/GetdateofLastrun').get().then(function
(data) {
var serverDate = new Date(data);
Console.log(data); //Invalid Date
}
Here the API call returns below data from server.
21-04-2017 20:34:30 UTC +05:30
JavaScript Date Objects don't support that format.
I would recommend https://momentjs.com/
"Parse, validate, manipulate, and display dates and times in JavaScript."
Angular directives for Moment.js can be found at: https://github.com/urish/angular-moment
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:
Create a Date with a set timezone without using a string representation
(29 answers)
Closed 8 years ago.
I'm working with an API that returns time values in the following format:
2013:02:27T06:39:25
Note the lack of any identifier for timezone.
From the API Docs:
https://partner-api.groupon.com/ledger
"Transaction timestamp of the ledger entry in the affiliate's time-zone.. The format is YYYY-MM-DDThh:mm:ss.. Example 2013:02:27T06:39:25"
Apparently the API response time zone is EST (the affiliate's time-zone). What is the best way to derive a UTC timezone value from this for storage in a MongoDB database.
The format is YYYY-MM-DDThh:mm:ss.. Example 2013:02:27T06:39:25"
It looks like there is a mistake in this documentation example as it doesn't match the suggested format (2013:02:27T06:39:25 should be 2013-02-27T06:39:25).
The sample response later on that page does match the expected format:
"orderDate": "2012-11-21T04:57:03"
I would suggest using moment-timezone -- it has a moment.tz() constructor which will parse the date string and set the expected time zone:
> var moment = require('moment-timezone');
> var orderDate = moment.tz("2012-11-21T04:57:03", "America/New_York")
> orderDate.toString()
'Wed Nov 21 2012 04:57:03 GMT-0500'
> orderDate.toISOString()
'2012-11-20T17:57:03.000Z'