how to modify date format in javascript (node js) - javascript

I have a date format like 2015-12-07T02:45:00.000Z but want to convert it to like Nove 12 , 2015 4:30 PM or 3 minuts ago accordingly

You can use a library called moment.js. And use
moment().format('MMMM Do YYYY, h:mm a') to convert to your format or
moment().startOf('hour').fromNow() to print out relative time.

Have you looked at moment.js? It has relative conversions which seem to be what you are looking for.

Related

Extra word between date and time with date-fns

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")

How to format this date so it's more readable?

I have date in this format:
2022-06-28T17:09:00.922108+01:00
I want to covert it into a more readable format.
I'm using javascript/react and I tried moment-js but it gives me "invalid format".
What are my other options of turning it into a normal date?
You can use this to format date:
const format = "DD-MM-YYYY"
moment(date).format(format)
or
moment().format('MMMM Do YYYY, h:mm:ss a'); // June 28th 2022, 9:49:49 pm
for more detail refer: https://momentjs.com/
If you are using moment.js you can convert this to any format you like using this doc
Sample code
moment('2022-06-28T17:09:00.922108+01:00').format("DD-MM-YYYY"); //28-06-2022
Kindly go though the moment.js features and documents. There is a lot more you can do with this like showing time, day of the week etc

Handle datetime timestamp javascript(angularjs) and mongo for slider

I'm using angularjs and mongodb.
I have not found a angularjs date slider so I'm taking this one. The idea is to use a timestamp with a step a day. So I take this timestamp convert it to a dd mmmm yyyy format and be displayed in the front-end.
Assuming as I said I use mongodb what can be a good solution for the date number format?
If you are looking for a date time library, try moment.js
It is very simple to use. Include the script file using <script> tag. Then use it like this.
moment().format('MMMM Do YYYY, h:mm:ss a');
This will return November 6th 2013, 1:35:00 pm
If you need to show the time in human format use fromNow() like this moment("20111031", "YYYYMMDD").fromNow(); This will result 2 years ago .

Split the date in javascript/jquery

How to split the below date format into day date and time.
Date Format is like "2013-05-07T11:04:00+05:30"
I want to display above date like "Tue,7 May 2013, 11.04AM".
Please suggest how to do this in java script or jquery
Thanks in Advance.
You can use this very useful little library:
http://momentjs.com/
Example:
moment("2013-05-07T11:04:00+05:30", "MMMM Do YYYY, h:mm:ss a");
You might be able to use Date.parse(string), but there is no way to determine what date/time formats a particular JavaScript implementation supports.
Otherwise:
Use a regex to break up (and validate) the string, convert each string component into a Number and then pass to the Date constructor taking separate components.
Use a library that implements the previous option (eg. see other answer).
You should work with the native Date Object - this is the easiest way to handle the string.
You can do the following:
var date = new Date("2013-05-07T11:04:00+05:30");
now you have several Date methods you can use to format your string and get the information you need, e.g. what you want is:
date.toUTCString()
// output:
"Tue, 07 May 2013 05:34:00 GMT"
You also could use a regex or an external library, but probably the best way (imo!) is to simply work with the Date Object.
moment.js could be a good choice for you, for example:
Actual moment:
moment().format('MMMM Do YYYY, h:mm:ss a');
Format:
moment("2013-05-07T11:04:00+05:30", "MMMM Do YYYY, h:mm:ss a");

Timeago + Localtime combined

I'm trying to combine timeago with datejs (with help of this to get format for local time)
for timeago I use the following:
jQuery(document).ready(function() {
jQuery("abbr.timeago").timeago();
});
For the localtime i use this:
jQuery(document).ready(function() {
$('.UTCTimestamp').localTimeFromUTC('MM/dd/yyyy hh:mm:ss');
});
How do I combine those two together? Right now I'm only able to use one at the time like this:
For Timeago:
<span class='UTCTimestamp'>2011-09-09 10:10:10</span>
and for localtime;
<abbr class='timeago' title='2011-09-09 10:10:10'>2011-09-09 10:10:10</abbr>
don't add any javascript code or jquery code except this;
$('.timeago').timeago();
and then add 'Z' (or including T). for more information go to this link
<abbr class='timeago' title='2011-09-09 10:10:10Z'></abbr>
http://en.wikipedia.org/wiki/ISO_8601#Combined_date_and_time_representations
I am trying to do the same thing - here is what I finally figured out.
My HTML has this:
<abbr class="timeago localtime" title="#Model.GetLastUpdatedDateTimeISO8601(category, report)">#Model.GetLastUpdatedDateTimeRFC1123(category,report)</abbr><br />
I then have these 2 chunks of javascript:
$('.localtime').localTimeFromUTC('MM/dd/yyyy hh:mm:ss a');
$('.timeago').timeago();
This is using ASP.NET MVC Razor syntax in the HTML, but basically what that does is get the date/time strings in two different formats. All times are stored in UTC. The timeago plugin uses the ISO 8601 formatted string to do its magic, and the localtime 'plugin' uses the RCF1123 format.
ISO8601 looks like this: yyyy-MM-dd HH:mm:ssZ
RFC1123 looks like this: ddd, dd MMM yyyy HH:mm:ss GMT
The final result is that on-screen I see timeago's "about 10 minutes ago", but when I hover I get "10/26/2011 08:57:43 PM".

Categories

Resources