I'm trying to get the difference between now and an end date in minutes. I have two date variables, but when i try to subtract them, it says Property 'diff' does not exist on type 'string'.
console.log(now) outputs: November 24th 2019, 11:13:32
console.log(endDate) outputs: November 24th 2019, 12:25:10
so the values are in date format.
I appreciate any help
let endDateTime = moment
.unix(parseInt(auctionEndDateTime))
.format("MMMM Do YYYY, h:mm:ss");
let start=moment(Date.now());
let end=moment(endDateTime);
let duration = moment.duration(end.diff(start));
let hours = duration.asHours();
console.log('hours ' + hours);
console.log('days ' + duration.asDays());
// you can get the difference between two days with javascript Date class
let firstDate = new Date('Sun Nov 24 2019 17:28:33'); //new Date('2019-11-12');
let secondDate = new Date('Tue Nov 26 2019 17:28:33');//new Date('2019-11-20');
let milliSFirst = firstDate.getTime();
let milliSSecond = secondDate.getTime();
console.log("diff in days " + (milliSSecond - milliSFirst)/(1000 * 3600 * 24) )
// take the dates as milliseconds and then you can do the calculations
let start=moment(Date.now());
let end=moment(Date.now() + 1000 * 3600 * 24 );
let duration = moment.duration(end.diff(start));
let hours = duration.asHours();
console.log('hours ' + hours);
console.log('days ' + duration.asDays());
<script src="https://rawgit.com/moment/moment/2.2.1/min/moment.min.js"></script>
Related
I have an ISO formatted date that isn't being converted to the correct time, and I can't figure out why.
const m = (() => {
console.log("Here is the original time: " + periods)
const timeFixed = moment(periods).format("dddd, MMMM Do YYYY, h:mm:ss ");
console.log("Here is the fixed time: " + timeFixed)
return timeFixed;
})();
Using this, returns this:
Here is the original time: 2020-08-12T08:52:55Z
Here is the fixed time: Wednesday, January 1st 2020, 12:00:00
Any help given is appreciated as always.
Solved it, weirdly. It seems Moment was having an issue with the incoming format. Converted it to a string, then it handled it.
const m = (() => {
console.log("Here is the original time: " + periods)
const timeString = new Date(periods);
const timeFixed = moment(timeString).format("dddd, MMMM Do YYYY, hh:mm:ss ");
console.log("Here is the fixed time: " + timeFixed)
return timeFixed;
})();
Resulted in:
Here is the original time: 2020-08-12T08:52:55Z
Here is the fixed time: Wednesday, August 12th 2020, 09:52:55
Yes the time zone is off, but that's another problem for another day. Thanks for helping me find it.
When converting time to UTC its showing one hour less than expected
I am updating a variable of dot net via moment to convert the time & show local system time to user. But post conversion i am getting one hour less. Tried utcOffset but getting error utcOffset is not a function. any suggestion
Where formData.SubmittedDate = "6/7/2019 5:44:59 AM"
$('[data-utcdate]').each(function () {
var d = moment($(this).attr('data-utcdate'));
//var isDST = d.utc().local().isDST();
//var d = moment(d).utcOffset(d);
d = d.utc();
$(this).html(d.format('MMM D, YYYY h:mm A'));
})
Getting :Jun 7, 2019 12:14 AM
Expected : Jun 7, 2019 11:44 AM
From the docs:
Get the UTC offset in minutes.
So you could use a manipulation method like add with it:
$('[data-utcdate]').each(function () {
var d = moment($(this).attr('data-utcdate'));
var offset = d.utcOffset() // will return the offset in minutes
var time = d.add(offset, "m");
$(this).html(time.format('MMM D, YYYY h:mm A'));
})
I have a timestamp (submitTime) which I need to check whether it is less than 1 hour old or not. Timestamps are in microseconds and including date.
currentTime = 1527530605357000000 (Monday, May 28, 2018 6:03:25.357 PM)
submitTime = 1527529918658907821 (Monday, May 28, 2018 5:51:58.659 PM)
long currentTime = (long) (new Date().getTime()*1000000)
submitTime = job.SubmitTime // part of the code
oneHhour = 3600000000
if (currentTime - submitTime > oneHhour) {
println job.Name + " env is up more than 1 hour";
But it doesn't work since the result is 686698092179 and it it not represent time.
Help?
Assuming SubmitTime is a timestamp in microseconds, you can compare it the the current timestamp in microseconds like so:
// Get the current time (System.currentTimeMillis) in microseconds:
long currentMicroseconds = TimeUnit.MILLISECONDS.toMicros(System.currentTimeMillis())
// You could also simply do this:
long currentMicroseconds = System.currentTimeMillis() * 1000
// Subtract the timestamps and compare:
if (currentMicroseconds - job.SubmitTime > 3600000000) {
// More than an hour has elapsed
}
The timestamp is assumed to be the number of microseconds since January 1, 1970, 00:00:00 GMT (consistent with Date.getTime).
In groovy you can use TimeCategory which is much more intuitive:
def date = new Date(timestampInLong)
use (groovy.time.TimeCategory) {
println (date > new Date() - 1.hour)
}
I am trying to return the month number passing the month name using MomentJS. For example if I pass "July" to moment() I would expect 7 to be returned.
After reading through the docs I tried several different ways, and this way came close...
console.log(moment().month("July"));
In the console, buried in the response I could see this...
_monthsParse: Array[7]
Could anyone please tell me how to return the month number using MomentJS correctly?
Try :
moment().month("July").format("M");
Relevant documentation: http://momentjs.com/docs/#/get-set/month/
alert(moment().month("July").format("M"));
<script src="https://momentjs.com/downloads/moment.min.js"></script>
Anybody looking to get month name from month number then you can try :
const number = 1; // 0 = Jan & 11 = Dec
moment().month(number).format("MMM"); // Feb
Use following to get full month name :
const number = 1; // 0 = January & 11 = December
moment().month(number).format("MMMM"); // February
To use simple month number try this:
const month = 2 //Feb
moment(month, 'M').format('MMMM');
##get month name in moment js with node js
moment() give today date
format("DD-MMMM-YYYY") / output 18-May-2020
format("DD-MM-YYYY") / output 18-05-2020
- sperator you can use /
```
var moment = require('moment');
m_date = moment().format("DD-MMMM-YYYY");
console.log("moment date :", m_date)
```
##output
```
moment date : 18-May-2020
```
Read Officail Docdescription here
I'm trying to get the difference between 2 dates in days, hours, and seconds:
import groovy.time.*
Date now = new Date()
// Using deprecated constructor just for this example
Date newYearsDay2000 = new Date(2000, 0, 1)
use (TimeCategory) {
now - newYearsDay2000
}
This prints:
-690023 days, -14 hours, -38 minutes, -27.182 seconds
Which is obviously nothing like the difference between today's date and 2000/1/1, where am I going wrong?
Thanks,
Don
Could be an issue with the deprecated constructor?
If you use Calendar (and the Groovy updated method) to create the newYearsDay2000 var, you get:
import groovy.time.*
import static java.util.Calendar.*
Date now = new Date()
// Use the static imported Calendar class
Date newYearsDay2000 = instance.updated( year:2000, month:JANUARY, day:1 ).time
use( TimeCategory ) {
now - newYearsDay2000
}
which gives the result:
3925 days, 23 hours, 59 minutes, 59.999 seconds
Edit
Yeah, the JavaDoc for Date shows that constructor with the comment:
Date(int year, int month, int date)
Deprecated. As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date) or GregorianCalendar(year + 1900, month, date).
Which leads me to believe that:
Date newYearsDay2000 = new Date(2000, 0, 1)
Is actualy creating the Date for new Years Day in the year 3900
Date
Parameters:
year - the year minus 1900.