This question already has answers here:
Moment.js - How to convert date string into date?
(3 answers)
Closed 3 years ago.
I am getting my date from PHP in format ymd. I am using Moment.js to convert it to format I need, but it's not working properly.
So for example today's date is comes like 190528 but after conversion to YYYY-MM-DD it becomes 190528-1-1 instead of 2019-05-28.
Is it the first format of the date which causes the problem, or there is a way to overcome this and convert it the way I need?
Parse Date first by passing format as 'YYMMDD'
moment("190528", "YYMMDD").format("YYYY-MM-DD");
This will give proper output.
You do not need to load moment for this.
You can break the string into parts of years, month and date. Format the year and return the joined string...
function formatDate(date) {
var _d = date.match(/.{1,2}/g);
_d[0] = "20" + _d[0];
return _d.join("-");
}
var d = "190528";
console.log(formatDate(d));
You need to tell moment what format your date string is in:
console.log(moment("190528", "YYMMDD"))
<script src="https://unpkg.com/moment#2.24.0/moment.js"></script>
You can pass the format of current date as a second param of moment.
console.log(moment("190528", "YYMMDD").format("YYYY-MM-DD"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
Here the docs.
Related
This question already has an answer here:
.diff is not a function on moments.js
(1 answer)
Closed 4 months ago.
I have a date "22-Mar" (YY-MMM) and I want to format it in "01.03.2022" and after that transform it into a date object without using "new Date()" method.
I heard that I can do this with MomentJS but I think I write something wrong.
I tried to format this date like that but this doesn't work.
moment(date, 'YY-MMM').format('MM/01/YYYY').toDate()
My error message:
TypeError: moment(...).format(...).toDate is not a function
How should I resolve the problem?
format() returns a string, where as the toDate() is only available on the MomentJS object.
So remove the format() if you want to convert it to a Date
If you use the 'format' to set the day to 1, you can use date() for that: .date(1)
Also, your custom format was invalid, I've changed it to DD-MMM to the date is proper parsed
const input = '22-Mar';
const mom = moment(input, 'DD-MMM').toDate();
console.log(mom);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
This question already has answers here:
How do I format a Microsoft JSON date?
(42 answers)
Closed 1 year ago.
receiving string of date that looks like this "/Date(1633421520000)/",
with moment I could just use it as moment("/Date(1633421520000)/") whats the equivalent for date-fns ?
for example differenceInMilliseconds how would i use it with received argument as this string "/Date(1633421520000)/"
not sure how to create my date object from this string so ill be able to use date-fns functions.
You need to extract the number (which looks like milliseconds since unix epoch) from the string:
"/Date(1634717139973)/".match(/\d+/)[0]
Then use Date constructor like so:
var date = new Date(Number("/Date(1634717139973)/".match(/\d+/)[0]));
date.toISOString(); // 2021-10-20T08:05:39.973Z
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)));