This question already has answers here:
Problem with date formats in JavaScript with different browsers
(4 answers)
Closed 7 years ago.
new Date().toLocaleString() --> "24/09/2015 10:14:00 PM"
new Date("2015-09-24 09:38:32.639").toLocaleString() --> "Invalid Date"
How can I format a date object from a timestamp in string format?
SOLUTION: At the end I got it fixed changing my date type in the server from DateTime to Instant, js will atomatically add zone offset automatically from a timestamp and will format the dates in the right way.
NOTE: I know this question is duplicated, however the solution proposed is different and may help other users to get a different approach to their code.
var myDate = "2015-09-24 09:38:32.639";
new Date(myDate.replace(/-/g,"/")).toLocaleString()
Now it's working fine
Related
This question already has answers here:
Converting Unix timestamp to Date Time String [duplicate]
(2 answers)
How do I format a date in JavaScript?
(68 answers)
Closed 3 years ago.
I want to know how to convert a long date like this 1542814586896 into a String format like this 2019/02/05
You can use Date class for setting time in integer format and getting any values like day, month, year
let date = new Date(1542814586896);
console.log(date.getDay(), date.getMonth(), date.getFullYear())
You can use
new Date(1542814586896).toLocaleDateString(`ja-JP`);
//-> "2018/11/21"
.toLocaleDateString() formats time into a format of a specific region. In the example above, time's formatted into Japanese format (just because it seems like in Japan they use exactly the format you need).
What's cool about this method is that you may just pass no argument to toLocaleDateString & it will then just automatically pick the format that the final user prefers (or more precisely, the format that is set in user's OS).
For example in my browser:
new Date(1542814586896).toLocaleDateString();
//-> "21/11/2018"
However, if I had Egyptian Arabic set as main language of my operating system, the result should be like:
new Date(1542814586896).toLocaleDateString();
//-> "٢١/١١/٢٠١٨"
You may find more information about different locales & corresponding formats here.
This question already has an answer here:
Moment returns invalid data even though date is correct
(1 answer)
Closed 4 years ago.
I have epoch time in array "task[i]["due_on"]=1533541797" which I am trying to convert to ISO 8601 using 'moment'. I have also tried in different ways but it always shows a different error.
var moment = require('moment');
var dueon_date = moment(task[i]["due_on"]).format();
console.log("date",dueon_date);
console logs output - Invalid date
Just specify the input format:
var dueon_date = moment("1533541797", "X").format();
console.log(dueon_date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script>
This question already has answers here:
javascript Date timezone issue
(3 answers)
javascript doesn't convert angular ui datepicker date to UTC correctly
(3 answers)
Closed 7 years ago.
We're storing birth dates in the format of yyyy-mm-dd. When this format is provided to the angular-bootstrap date picker, it selects the incorrect date in the popup. Converting it to a date object causes both the display and selection to be incorrect. See my plnk for examples (ignore validation stuff, that's a whole other issue).
// Displays '2015-09-25', but 24th is selected
var date = '2015-09-25';
// Displays '2015-09-24', selects 24th
var date = new Date("2015-09-25");
From javascript Date timezone issue: "In JavaScript, a value in the format of YYYY-MM-DD is interpreted as a UTC value, rather than a local-time value."
One workaround is to replace the hyphens with slashes:
var s = "2015-09-25";
var dt = new Date(s.replace(/-/g, '/'));
I would recommend using moment.js though. It works for me and I had the same problem with a javascript datepicker.
var s = "2015-09-25";
var dt = moment(s, 'YYYY-MM-DD').toDate();
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to format a JSON date?
I have a JSON that contains some dates that I need to be in a UTC format.
At present if I alert the dates out they are in the following format:
/Date(1329314400000)/
I am trying to loop round the JSON but am unsure on how to convert the above date format to UTC.
If anyone has any advice I would greatly appreciate it.
Check this out: How do I format a Microsoft JSON date?
var date = new Date(parseInt(jsonDate.substr(6)));
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Help parsing ISO 8601 date in Javascript
I think this should be very simple but turned out amazingly tedious.
From WEB API, I received selected object via ajax, and one of its properties is InspectionDate datetime string such as 2012-05-14T00:00:00
In javascript, I use following code to have correct date object
selected.JsInspectionDate = new Date(selected.InspectionDate);
But JsInspectionDate shows
2012/05/14 00:00 in firefox,
2012/05/13 20:00 in chrome and
NAN in IE9
for 2012-05-14T00:00:00.
Could someone tell me why this problem occurs? And how to fix this issue? I just want to show as in firefox for all browsers.
Do this:
new Date(selected.InspectionDate + "Z")
Rationale: Your dates are in ISO 8601 form. Timezone designators like "Z", a very short one for UTC, work.
Note! IE might not understand ISO 8601 dates. All bets are off. In this case, better use datejs.
Update:
First as one suggested, I tried following after referencing date.js.
selected.JsInspectionDate = Date.parse(selected.InspectionDate);
It seemed like working but later I found it was not enough since the JSON date string can have a format of 2012-05-14T00:00:00.0539 which date.js can't process either.
So my solution was
function dateParse(str) {
var arr = str.split('.');
return Date.parse(arr[0]);
}
...
selected.JsInspectionDate = dateParse(selected.InspectionDate);
FIDDLE
var selectedDate='2012-05-14T00:00:00';
var formatttedDate=new Date(selectedDate.substr(0,selectedDate.indexOf('T')));
document.write(formatttedDate.getFullYear()+'/'+(formatttedDate.getMonth()<10?'0'+formatttedDate.getMonth():formatttedDate.getMonth())+'/'+formatttedDate.getDay());