How to convert utc to string in momentjs? - javascript

I want to use moment for this.
moment("date","YYYYMMDD").fromNow(); // ~~ years ago example
I get UTC from MongoDB createAt. but I don't know convert UTC to string (variable date).
ex) mongodb_createAt = 2018-09-18T12:22:19.491Z
this mongodb_createAt convert to string(date)
I want to utc convert to date string..
I want to display ~~ years ago (like youtube)
How to ??
thanks.

Look at moment documentation https://momentjs.com/docs/#/displaying/fromnow/
Try moment().fromNow(); it should output ... years ago

You could use momentjs's .humanize() method to achieve this.
First, you'll need to calculate the duration between your timestamp 2018-09-18T12:22:19.491Z and the current time. Once you have this duration, you can then make use of .humanize() like so:
var mongodb_createAt = '2018-09-18T12:22:19.491Z';
var millisecondsElapsed = moment(mongodb_createAt).diff(moment());
var displayString = moment.duration(millisecondsElapsed).humanize(true);
console.log(displayString) // eg 17 hours ago
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
Note that the true argument passed to .humanize(true) adds the tense to the displayString string - ie true causes a result like "17 hours ago" rather than just "17 hours"
For more information on the humanize() method, see the official documentation

Related

Format time in JS

So I'm making a userinfo command
I use moment.js for relative time
moment(timestamp).fromNow() \\ 2 years ago
Is there a way to format it like 2 years x days, x seconds ago etc
I don't think there's an easy way to do this with Moment.js.
However, using Luxon (successor to Moment.js) you can create an interval, convert it to a duration then format the values. E.g. to get the years, months and days from the ECMAScript epoch to now:
// Create an interval from 1 Jan 1970 UTC to now
let interval = luxon.Interval.fromDateTimes(
new Date(0), // 1 Jan 1970 UTC
new Date() // now
);
// Show default interval object
console.log(interval.toFormat('yyyy-MM-dd'));
// Convert to duration object
let d = interval.toDuration(['years','months','days']).toObject();
// Display in friendly format
let text = [];
d.years? text.push(d.years + ' years') : null;
d.months? text.push(d.months + ' months') : null;
d.days? text.push(d.days.toFixed(1) + ' days') : null;
console.log(text.join(', '));
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/2.3.0/luxon.min.js"></script>
Note there is also:
Luxon interval human readable
Difference between two dates in years, months, days in JavaScript
How to get difference between 2 Dates in Years, Months and days using moment.js
and many others.
You mean, like moment.fromNow()
[Sometimes, it pays to read the documentation]
If that won't work for you, then something like
https://www.npmjs.com/package/pretty-ms
might.
But...
Consider migrating to Luxon — Moment.js is deprecated. Luxon is smaller, more elegant. More to point, it has a Duration that has a toFormat() method that will give you exactly what you are looking for.

Date & Time conversion from 2018-05-29T04:12:35Z to something else

Is it possible to convert the date & time format i.e. 2018-05-29T04:12:35Z in the format like 1 hour ago or something like this.
You can use momentjs for this
moment("2018-05-29T04:12:35Z").fromNow() //an hour ago
See this for reference
Try this.
console.log(moment("2018-05-29T04:12:35Z").fromNow());
console.log(moment(new Date()).fromNow());
console.log(moment("2018-05-29T05:12:35Z").fromNow());
<script src="https://cdn.jsdelivr.net/momentjs/2.13.0/moment.min.js"></script>
and you can get more details if you search moment.js from now in google

Turn a String to a 24 hour time format, using moment.Js

I am trying to format a String into a 24 hour format using moment JS at the moment I am not getting the expected output, the below explains it in more detail:
How I am coverting
var testFormat = moment("9:00","HH:mm:ss");
I was hoping this would output:
09:00:00 (24 Hour format)
But for some reason is does not convert just the date, this is outputing the following:
1471507200000
You are using it wrong. You can provide a format as the second argument to moment. So in your case it would be:
var myMoment = moment("9:00","H:mm");
To get the moment. Now you can format it the way you want:
myMoment.format("HH:mm:ss");
DEMO
moment("13:15:00", "h:mm:ss").format("hh:mm A");
it will give 12 hrs format

moment.js returning an unexpected fromNow date when date is MM/DD/YYYY HH:mm:ss

I have a date in the following format var timestamp = "6/9/2016 1:47:31 PM";. I'm trying to get the relative time (4 hours ago, 3 minutes ago, 3 days ago, etc...) from the timestamp compared to the current datetime using from now.
var LastReading = moment(timestamp).fromNow();
but this is returning "2010 years from now". I tried using the format
var LastReading = moment(timestamp, "MM/DD/YYYY HH:mm:ss").fromNow();
but I get the same result. Any ideas? Do I need to format the date in a different way in order to get the fromNow method to work as expected?
To match your timestamp, the format should look like this:
MM/DD/YYYY hh:mm:ss A
HH means 24 hour time, but you're using 12 hour time, for which you need to use hh. Also, A will match AM/PM.

How to get difference of saved time and current time in jquery?

I want to get the time difference between saved time and current time in javascript or jquery. My saved time looks like Sun Oct 24 15:55:56 GMT+05:30 2010.
The date format code in java looks like
String newDate = "2010/10/24 15:55:56";
DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = format.parse(newDate);
How to compare it with the current time and get the difference?
Is there any inbuilt function in jquery or javascript??
Any suggestions or links would be appreciative!!!
Thanks in Advance!
Update
Date is stored as varchar in the DB. I am retriving it to a String variable and then change it to java.util.Date object. The java code looks like
String newDate = "2010/10/24 15:55:56";
DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = format.parse(newDate);
This date object was sent to client. There i want to compare the saved date with current date and want to show the time difference like 2 secs ago, 2 hours ago, 2 days ago etc... like exactly in facebook. I have gone through some date to timestamp conversion tutorial in java script and now i can get the difference in timestamp. Now, i want to know how i shall change it to some format like "2 secs or 2 days or 24 hours"??. Or, how i shall change it back to date format???
Convert them into timestamps which are actually integers and can get subtracted from each other. The you just have to convert back the resulting timestamp to a javascript date object.
var diff = new Date();
diff.setTime( time2.getTime()-time1.getTime() );
You dont need to explicit convert, just do this:
var timediff = new Date() - savedTime;
This will return the difference in milliseconds.
jQuery doesn't add anything for working with dates. I'd recommend using Datejs in the event that the standard JavaScript Date API isn't sufficient.
Perhaps you could clarify exactly what input and output you're aiming for. What do you mean by "the difference?" There is more than one way to express the difference between to instants in time (primarily units and output string formatting).
Edit: since you said you're working with jQuery, how about using CuteTime? (Demo page)

Categories

Resources