Momentjs date value convert into datetime format - javascript

I have used moment js in date & time formatting, to be in a readable and string(month) manner. Now I am trying to get the same original formate here:
My original value: 2019-04-23T19:17:48.000Z
After moment:
moment('2019-04-23T19:17:48.000Z').format('dddd Do YYYY, h:mm:ss a')
output:
Wednesday 24th 2019, 12:47:48 am
now all I need my original value back by using the output here.
Any lead appreciates for the answer.

With only the data you're providing as an output of Wednesday 24th 2019, 12:47:48 am you can't really get back to your original value. If it were a valid date, there's not a month. You'd need logic to determine what 24ths of each month in 2019 are a Wednesday.
After some testing - there's a few things you need in order to get this string back into a date that Moment can recognize
moment('2019-04-23T19:17:48.000Z').format('dddd MMMM D YYYY, h:mm:ss a')
moment('Tuesday April 23 2019, 7:17:48 pm').format()
You have to have the month, otherwise it thinks its an invalid date. That can be the string of the month or just the number 4 in this case.
Moment won't take the rd from 23rd otherwise it's an invalid date.
You can either use some string transformations to get it back into a useable date object or add on a little extra data like the month and when you go to use that date format,
"Tuesday April 23 2019, 7:17:48 pm".replace('April ', '')

Related

Get the current date using date-fns in ISO-8601 format

I want to get a ISO-8601 format of last hour:minute:secounds from the current day using date-fns lib:
I'm using:
endOfDay(new Date());
Fri Sep 14 2018 23:59:59 GMT-0300
So add toISOString() to get it in ISO format:
endOfDay(new Date()).toISOString()
Result:
2018-09-15T02:59:59.999Z
When I need:
2018-09-14T23:59:59.999Z
2018-09-14T23:59:59.999Z is not the end of day computed by endOfDay(new Date());. It is 3 hours earlier. The "Z" means UTC and your local time zone has the offset to UTC -0300.
By executing endOfDay you get a date value, whcih you can use in comparisons and other computations with other date values. It is a complete date with time in the local time zone.
Do you want to retain the same day number in the formatted string? You can format the date to an ISO 8601 string in your local time zone:
format(endOfDay(new Date()), 'YYYY-MM-DD[T]HH:mm:ssZZ')
// Prints "2018-09-14T23:59:59.999-0300" in Brazil (BRT)
Do you want to get the end of the day with the same number as today in UTC? If you disregard the actual end of your day in relation to other dates, you can just concatenate the "last second of the day" with "Z" to it:
format(new Date(), 'YYYY-MM-DD') + 'T23:59:59.999Z'
// Prints "2018-09-14T23:59:59.999Z" anywhere on Earth

moment.js uses the month as day instead the day

I'm using currently moment.js (moment-with-locales.js - v.2.14.1) in my project. I want to remove the time of my datetime string to get only the date. But if I use the .format() method of moment.js I got an incorrect date.
I want to format this datetime string:
from ' 08.10.2016 11:00 ' to ' 08.10.2016 '
Here is a snipped that I used in my angular project:
var date = moment('08.10.2016 11:00').format('DD.MM.YYYY')
console.log(date)
If I run this I got this output
10.08.2016
instead of
08.10.2016
The funny thing is, if I want to get the timestamp (milliseconds) of my datetime string, it works perfect. Example:
var dateStart = moment('08.10.2016 19:00', 'DD.MM.YYYY HH:mm').valueOf()
console.log(dateStart)
Will return
1475946000000 -> Sat Oct 08 2016 19:00:00 GMT+0200
How can I get the correct Date?
It depends on your locale. en-US locate means moment will parse by "month day year". So, you need to parse with the pattern as well:
var date = moment('08.10.2016 11:00','DD.MM.YYYY HH:mm').format('DD.MM.YYYY')

Formatting time using moment JS shows incorrect Date

I have time coming from server in certain format for example time is (1473286826319). Using MomentJS library
moment(1473286826319).format()
// output 2016-09-07T16:20:26-06:00 i.e sep 7 2016
However when I use custom formating style as below
moment(1473286826319).format('dddd, MMMM d, YYYY, h:mm A');
// output Wednesday, September 3, 2016, 4:20 PM
I have the date going back from Sept 7 to Sept 3 and rest of the data is accurate. Is there something I am doing wrong?
You are using a small d which is the Day of Week. If you want the Day of Month use a D or a DD for leading zeros.
Checkout the documentation

Getting current date with javascript with HTTP Date format

i'm looking for a way to get the current date and time in the HTTP date format which is for example "Tue, 15 Nov 1994 08:12:31 GMT". I would like to get that with JavaScript.
I tried with:
new Date().toString()
but this gives me a different format like: "Tue Aug 20 2013 00:19:28 GMT+0200".
I would need to invert the month with the day and put a coma between the day of the week and the day of the month.
How can i get that format?
The HTTP date format you mention is actually an RFC-1123 timestamp. The toUTCString function on the Date object is supposed to return a compatible value.
You can validate this with this sample Fiddle.

Laravel Angular how to filter date on Carbon String?

how can you filter date on a JSON time like created_at like 2016-02-29 19:20:00 ?
The Angular filter only works on timestamp.
How can i change the output to the timestamp?
If I understand your question you want to do manipulate the created_at time or w/e time. I would highly suggest using moment.js for this.
Few examples from their page.
moment().format('MMMM Do YYYY, h:mm:ss a'); // February 28th 2016, 2:17:50 am
moment().format('dddd'); // Sunday
moment().format("MMM Do YY"); // Feb 28th 16
moment().format('YYYY [escaped] YYYY'); // 2016 escaped 2016
moment().format();
Same problem with me, below my solution:
In Json:
data[i].itemDate = data[i].itemDate.replace(/(.+) (.+)/, "$1T$2Z")
data[i].itemDate = new Date(data[i].itemDate);
Now you can use Angular filter
if you use YYYY-MM-DDTHH:mm:ss.sssZ time format you must be shure in a itemDate in UTC time zone. If your itemDate in GMT+0200 (for example) just drop "Z" from end, it helps to me :)
itemDate.replace(/(.+) (.+)/, "$1T$2")

Resources