The service returns a past date-time in ISO 8601 format like 2018-03-23T08:00:00Z and a duration in PnYnMnDTnHnMnS format like: PT3H. From those 2 values I need to calculate the date-time of the end of the duration. So in my case it would be 2018-03-23T11:00:00Z.
I'm using moment.js for this. The problem is, when I'm tring to get the duration end date-time it returns a human readable string like in xy hours.
The problems I'm facing:
It returns wrong duration. If I have PT3H it should return in 3 hours but it returns in 9 hours instead.
The end date-time should be in milliseconds, currently its in "human readable form"
My code:
let dur = moment.duration("PT3H");
let myDate = moment("2018-03-23T08:00:00Z");
myDate.from(dur); // It returns "in 9 hours"
JsBin
You can add your duration to your date using add(Duration) and then use from.
Note that from accepts: Moment|String|Number|Date|Array while, in your code, you are passing a duration (dur).
You can use diff to get the difference in milliseconds.
If you want to get Unix timestamp of a moment object you can use valueOf().
Here a live sample:
let dur = moment.duration("PT3H");
let myDate = moment("2018-03-23T08:00:00Z");
let m2 = myDate.clone().add(dur);
console.log(m2.from(myDate));
let diff = m2.diff(myDate);
console.log(diff);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js"></script>
Related
I am new in this library. I have 2 dates, and I would like to obtain the difference in days, minutes and hours between both. but for some reason the output is an erroneous data. I have based on this page
to get the exact difference and it is different to my result. what am I doing wrong?
var now=2018-07-04 00:37:02;
var then=2018-07-05 08:00:00;
moment.utc(moment(now,"YYYY/MM/DD HH:mm:ss").diff(moment(then,"YYYY/MM/DD
HH:mm:ss"))).format("HH:mm:ss");
output is : 16:37
There are two significant problems here:
a.diff(b) returns a - b, so it will be positive if a is later than b. You're using now.diff(then) effectively, so that will currently give a negative difference, and I suspect you're expecting it to be positive.
You're turning a duration in time into a date/time using moment.utc. The value just isn't a date/time, so shouldn't be treated as one. Instead, use moment.duration(...) to convert to a momentjs Duration object, which you can then handle however you want.
Here's a complete example, having broken out each of the conversions involved for readability:
var now = "2018-07-04 00:37:02";
var then = "2018-07-05 08:00:00";
var parsedNow = moment(now, "YYYY/MM/DD HH:mm:ss");
var parsedThen = moment(then, "YYYY/MM/DD HH:mm:ss");
var diff = parsedThen.diff(parsedNow);
var durationDiff = moment.duration(diff);
alert(durationDiff.toISOString());
Output: PT31H22M58S
That 31 hours is 1 day + 7 hours etc. While a duration has the concept of days, months, years etc, I don't see any way of normalizing a duration created from diff to "1 day 7 hours". (It could certainly be done, but only as far as days - you couldn't normalize to include months without providing more context.)
Hi im currently using momentjs for my dates in my project and im having a bit of trouble when subtracting 2 dates.
Here are my sample dates:
2016-10-08 10:29:23
2016-10-08 11:06:55
ive tried using the diff and subtract from the guide docs of momentjs but i got nothing.
And what if the date subtracted dates are more than 24 hours?
Thnks in advance.
You are correct, you can use moment's diff function to subtract two dates (see my example on Plunker):
var date1 = moment('2016-10-08 10:29:23');
var date2 = moment('2016-10-08 11:06:55');
var diff = date2.diff(date1);
Diff will be equal to 2252000, the number of milliseconds between the two date. See documentation for more details.
You can pass a second argument to diff with the measurement to use (years, months, weeks, days, hours, minutes, and seconds), so if you want to know the number of minutes between the two dates you can write:
var diffInMinutes = date2.diff(date1, 'minutes');
And you get 37 minutes.
I have to calculate the duration of two dates with days and hours remaining.
If the two dates have different days, I need to return the duration in days and hours.
For example, given the following input:
2016-12-11T09:30:00.000Z and 2016-12-12T11:30:00.000Z
I would like to have this output:
1 day 2 hrs
How to achieve this using moment.js?
You can use moment-duration-format plug-in.
Just create moment object from your strings/dates, then get the difference in milliseconds using diff method in order to create a duration object. Use format method from moment-duration-format to print duration according your needs.
Here a working example:
// Create moment objects
var m1 = moment('2016-12-11T09:30:00.000Z');
var m2 = moment('2016-12-12T11:30:00.000Z');
// Get the difference in milliseconds
var diff = Math.abs( m1.diff(m2) );
// Format duration according your needs
console.log(moment.duration(diff).format("d [day] h [hrs]"));
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-duration-format/1.3.0/moment-duration-format.min.js"></script>
I'm trying to get acquainted with weatherData in R.
Having downloaded a set of temperature data I've then exported it to CSV.
Opening the CSV in Libre Calc shows the date and time for each temperature reading as a string of ten digits. In spite of some Googling I have not found a way of successfully converting the string into the format in which it appears in R.
For example: 1357084200 I believe should translate to 2013-01-01 23:50:00
Any help in getting the correct date in the same date format to appear in Calc via the CSV greatly appreciated.
Here is the direct way:
as.POSIXct(1357084200, origin="1970-01-01", tz="GMT")
#[1] "2013-01-01 23:50:00 GMT"
If it's really a character:
as.POSIXct(as.numeric("1357084200"), origin="1970-01-01", tz="GMT")
I'm not aware of a direct way of doing this, but I believe I've figured out a workaround.
For starters your example is correct. The long number (timestamp) is the number of seconds passed since 1970-01-01 00:00:00. Knowing this you can actually calculate the exact date and time from the timestamp. It's a bit complicated due to needing to take into account the leap years.
What comes in handy is the ability to supply an arbitrary number of days/months/years to LibreOffice function DATE. So in essence you can find out the number of days represented in timestamp by dividing it by 60*60*24 (number of seconds in a minute, number of minutes in an hour, number of hours in a day). And then supply that number to the date function.
timestamp = 1357084200
days = timestamp / FLOOR(timestamp / (60*60*24); 1) // comes out at 15706
actualdate = DATE(1970; 1; 1 + days) // comes out at 2013-01-01
seconds = timestamp - days * 60 * 60 * 24 // comes out at 85800
actualtime = TIME(0; 0; seconds) // comes out at 23:50:00
Then you can concatenate these or whatever else you want to do.
I have an array of time strings, for example 115521.45 which corresponds to 11:55:21.45 in terms of an actual clock.
I have another array of time strings in the standard format (HH:MM:SS.0) and I need to compare the two.
I can't find any way to convert the original time format into something useable.
I've tried using strptime but all it does is add a date (the wrong date) and get rid of time decimal places. I don't care about the date and I need the decimal places:
for example
t <- strptime(105748.35, '%H%M%OS') = ... 10:57:48
using %OSn (n = 1,2 etc) gives NA.
Alternatively, is there a way to convert a time such as 10:57:48 to 105748?
Set the options to allow digits in seconds, and then add the date you wish before converting (so that the start date is meaningful).
options(digits.secs=3)
strptime(paste0('2013-01-01 ',105748.35), '%Y-%M-%d %H%M%OS')