I am trying to convert string "12012019" to date format using momentjs instead of doing any string manipulation.
Does anyone have any idea or suggestion?
You can read the doc
moment('12012019', 'MMDDYYYY') // Moment instance, you can call .toDate() if you want to convert to native Date
Related
I'm making an api call which retrieves a set of objects. One of the objects returns a date and time together like this:
createdAt: "2020-11-04 09:48:32"
This is where I display the date and time:
<template v-for="item in collectedTrash">
<BeforeAndAfter v-if="item.isPresented === 1"
:key="item.username"
:avatarUrl="require('#/assets/img/images/img_stats_km#2x.png')"
:imageBefore="getImageUrl(item.imageUrlBefore)"
:imageAfter="getImageUrl(item.imageUrlBefore)"
:username="item.username"
:date="item.createdAt"/> This is where I get the date
</template>
Is there anyway that I can retrieve the date only, rather than the date and time?
I am assuming you don't have access to the api and therefore have to process the date in your vue application.
Do you need the date as a date Object or is a string fine?
If a string representation is enough, you could use a library such as Date fns and use the format function:
...
:date="format(new Date(item.createdAt), 'yyyy-MM-dd')"
Another option might be to only use the first 10 characters of the string you received as the date: date="item.createdAt.substring(0,9)"
Did you try something like: :date="item.createdAt.substr(0, 10)"
In my experience the easiest way to format datetimes in JS it to use the Moment.js library (https://momentjs.com/). You could reformat the datetime string before passing it to your child component.
Or, if you don't want to rely on a third-party library and you know that the datetime string will always be passed in that format, I suppose you could do item.createdAt.slice(0, 10).
Moment.js can be very helpful with dates and times
Moment Docs
moment("String").format('L'); // 04/11/2020
I have a date which is like this "19/05/2020". I am trying to convert it into "yyyy-MM-dd" format using pipe. So the date should be look like "2020-05-19".
this._datePipe.transform("19/05/2020", 'yyyy-MM-dd');
It is giving me an error saying Unable to convert "19/04/2020" into a date' for pipe 'DatePipe
Also tried converting the string to date but again invalid date message is getting display.
Is there any way so that we can convert this date that is "19/05/2020" to a valid one so that we can perform datepipe on it.
I guess you need to convert your date having a supported formatted string :
For example, "2011-10-10" (date-only form), "2011-10-10T14:48:00" (date-time form), or "2011-10-10T14:48:00.000+09:00" (date-time form with milliseconds and time zone) can be passed and will be parsed
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
This is valid for Date.parse() or new Date().
So you'll need to parse your string with another method.
This may helps you :
How to convert dd/mm/yyyy string into JavaScript Date object?
I am using JSON.strigify to convert JSON object to a string, but in the process, it is also converting the local date to UTC date hence returning the date in the following format.
"2017-04-30T18:30:00.000Z"
I don't want it to happen and want to keep the date as is.
You can try of this Options
new Date("2017-04-30T18:30:00.000Z");
It Gives you date format you want
As the title suggests, I need to know how to turn strings in the format of "10/30/2015 2:43pm" into JavaScript Date objects. The string comes from Datepair (http://jonthornton.github.io/Datepair.js/) and I need to insert it into Mongo DB as a date. I've got Moment.js too if that's useful in this situation.
Thanks!
Just try with:
new Date("10/30/2015 2:43pm".replace(/([ap]m)$/, " $1"))
Using moment, just provide the format as second parameter:
moment("10/30/2015 2:43pm", "MM/DD/YYYY hh:mma");
I'm trying to parse the YYYYMMDDTHHMMSSZ format with moment.js, but I always get "Invalide date".
Is there a way to parse it?
moment takes a second parameter that indicates the format, so you can do:
var dtg = '20140112T121537Z';
moment(dtg, 'YYYYMMDDTHHmmssZ');