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
Related
I am looking for a way to add an extra word between the date and time in date-fns library, but couldn't find such format. Right now I am using format function with MMM d hh:mm aa format.
The outcome is:
Nov 15 10:00 PM
Is there a way to get:
Nov 15 at 10:00 PM
I'd highly recommend reading and understanding the docs, before posting a question as simple as this.
As per documentation for the latest version (as of writing 2.29.3) the following will yield the required result.
format(new Date("Sep 2 2020 13:00"), "MMM dd 'at' HH:MM a")
In this example on CodeSandbox I'm trying to get the date formatted as dddd, MMMM Do and the expected result should be like Thursday, January 13th but instead is 0013, January 13th
As per date-fns documentation here Date-fns format, writing dddd should give me back the name of the day but I got instead 0013 and I don't understand why.
I need help on what is wrong with the way I'm writing the date format and get the name of the day instead of that 0013.
You are using a new version of date-fns https://date-fns.org/v2.16.1/docs/format.
You may use format="iiii, MMMM Do".
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')
output:
Wednesday 24th 2019, 12:47:48
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 ', '')
Here are the best solution for the date formate under UTC/GMT:
let dateTime = new Date("2015-09-30T19:54:21.000Z");
dateTime = moment(dateTime).utc().format("YYYY-MM-DD HH:mm:ss");
moment(moment(dateTime).utc().format("YYYY-MM-DD HH:mm:ss")).format();
o/p => "2015-09-30T14:24:21+05:30"
moment("2015-09-30T14:24:21+05:30").format("YYYY-MM-DD HH:mm:ss")
o/p => "2015-09-30 14:24:21"
moment.tz('2015-09-30 14:24:21', 'YYYY-MM-DD HH:mm:ss', 'UTC').format()
final o/p => "2015-09-30T14:24:21Z"
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")
I have a form where the use can save a schedule date. I want to be able to send to my backedn the date to utc format. I have something like :
<input type="hidden" name="scheduled_publication[publication_date]" value="2015/11/27">
<input type="hidden" name="scheduled_publication[time]" value="13:00">
But the problem is if I change this value with the browser timezone offset, I have also to change the date value sometimes. A schedule at 01am with -02:00 time offset will change the date.
Does the easiest scenario is to get the two current values and process them with momentjs?
Other question. How to display the time with the correct timezone with momentjs. The raw date rendered looks like : 2015-11-16 16:47:29 +0100. I've try using .utc method.
> moment('2015-11-16 16:47:29 +0100').format("dddd, MMMM Do YYYY ha")
"Monday, November 16th 2015 4pm"
> moment.utc('2015-11-16 16:47:29 +0100').format("dddd, MMMM Do YYYY ha")
"Monday, November 16th 2015 4pm" #should be 3pm
Why local is not applied on the first command ? When I do
> moment('2015-11-16 16:47:29 +0100')
Moment {_isAMomentObject: true, _i: "2015-11-16 16:47:29 +0100", _isUTC: false, _locale: Locale, _d: Mon Nov 16 2015 16:47:29 GMT+0100 (CET)…}
Timezone seems to be recognised.
When you work with times at the end it's difficult to stay consitent.
EDIT:
With the comments and answer. First with the data insert by pickadate and pickatime with EST as computer time, I set the value (20 november at 23 hour) and convert them.
> var date = $("[name='scheduled_publication[publication_date]']").val()
> var time = $("[name='scheduled_publication[time]']").val()
> $("[name='scheduled_publication[time]']").val(moment(date + ' ' + time).toISOString())
[<input type="hidden" name="scheduled_publication[time]" value="2015-11-18T14:00:00.000Z">]
Except this quite ugly javascript. It seems good for me. The value return is in UTC. Perfect for my database.
Try to use ISO date for storing dates on back-end:
moment().date(23).month('August').year(2015).hour(15).minute(45).second(0).millisecond(0).toISOString();
// output: 2015-08-23T10:15:00.000Z
or
moment('23 aug 2015, 3:45pm', 'DD MMM yyyy, hh:mma').toISOString();
// output: 2015-08-23T10:15:00.000Z
Notice that time-zone info got dropped (Z means its in UTC). Now, displaying it back:
moment('2015-08-23T10:15:00.000Z').toString();
// output: Sun Aug 23 2015 15:45:00 GMT+0230
You see ? GMT+0230 got automatically there, because that's current machine's offset.
Formatted display:
moment('2015-08-23T10:15:00.000Z').format('dddd, MMMM Do YYYY h:mma');
// output: Sunday, August 23rd 2015 3:45pm