moment js how to 24 hours - current time - momentjs

I want to calculate 24 hours - current time so I did like this with moment.js
moment('24:00:00').subtract(now, 'HH:mm:ss').format("HH:mm:ss")
but I recive
Invalid date
Help me

I resolved it like this
moment('24:00:00', 'HH:mm:ss')
.subtract(moment().format("HH"), 'hours')
.subtract(moment().format("mm"), 'minutes')
.subtract(moment().format("ss"), 'seconds')
.format("HH:mm:ss")

Related

Incorrect seconds difference between two TimeSpan

I am trying to find the difference in two timestamps, in one of my project.
I have a timestamp on which touches on the screen and I subtract it from DateTime.Now
Here is how I am doing it-
var sec = DateTime.Now.TimeOfDay.Subtract(TimeOfUserTouch)
TimeOfUserTouch is also a TimeSpan object.
Console.WriteLine("Time Now {0} \n\n Earlier Time {1} \n\n Difference {2}",DateTime.Now, TimeOfUserTouch, sec.Seconds);
When I printed the output, here is how it shows-
Time Now: 12/8/2020 4:38:46 PM
Earlier Time: 12/8/2020 4:37:56 PM
Difference: 14 sec
Now this difference is clearly wrong. Ideally it should return 50 sec.
Could you guys explain what exactly is going wrong here?
Answering my own question in case someone else runs into the same problem -
I ended up using using TotalSeconds instead of secondsas suggested by #Gevorg in the comment, which fixed the issue.

Momentjs - cannot set hour and minutes in UTC

Why
moment().utc("14:30", "HH:mm").format()
gives me
2019-06-07T00:56:14Z - my local time
instead of
2019-06-07T14:30:14Z
Thank you in advance for help.
Use the moment without () like this moment.utc("14:30", "HH:mm").format()

Convert into am / pm in Impala

Like in a similar query on this forum I need, but I need it to work in Impala:
In a workaround my colleague and myself attempted the following:
-- combine start date and time into a datetime
-- impala can't handle am/pm so need to look for pm indicator and add 12 hours
-- and then subtract 12 hours if it's 12:xx am or pm
================
t1.appt_time,
hours_add(
to_timestamp(concat(to_date(t1.appt_date),' ',t1.appt_time),'yyyy-MM-dd H:mm'),
12*decode(lower(strright(t1.appt_time,2)),"pm",1,0) -
12*decode(strleft(t1.appt_time,2),'12',1,0)
) as appt_datetime,
t1. ...
=========
Has anybody an easier and more elegant approach ?
Your workaround is valid, Impala does currently support AM/PM formatting for dates. There are a few open issues related
https://issues.apache.org/jira/browse/IMPALA-3381
https://issues.apache.org/jira/browse/IMPALA-5237
https://issues.apache.org/jira/browse/IMPALA-2262

Customize certain momentjs humanized duration values

Short question: I need momentjs to humanize 60 minutes into 1 hour instead of an hour. Can't figure out how to get it to work.
Long question:
Just started using momentjs, works great. We are using it to display how often a dashboard is updated.
The timers are set as a integer in minutes. We are using the humanize moment option to display 30 as 30 minutes and 360 as 6 hours etc.
This works great but not in 2 cases. 60 gets humanized to an hour. We need it to be 1 hour. And 1440 is displayed as a day, instead of 1 day.
We need this change because the column is answering the question "How often does your metric update?"
The answer is "every 1 hour". "every an hour" doesn't quite work.
I read through the docs and googled, but couldn't find a way to customize just a few humanized display formats.
We are already setting true as the second parameter to get just the value without the suffix from this question and answer - How to Customize Humanized Moment js Date Result
But the value comes back as 'an hour' instead of '1 hour'.
You can use updateLocale method documented in the Customize -> Relative time section of the docs. This will affect output of from, fromNow, to, toNow and humanize.
In your case you can simply update h and d keys of the relativeTime object.
Here a working sample:
moment.updateLocale('en', {
relativeTime : {
h: "1 hour",
d: "1 day",
}
});
var d1 = moment.duration(60, 'm');
var d2 = moment.duration(1440, 'm');
console.log(d1.humanize());
console.log(d2.humanize());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>
You can find further examples of relative time customization here and here.
you can use the methods like minutes(), months(), years() available on the duration object and then you can create your own string. the humanize() method gives an approximate value against the duration and not the exact duration.

Momentjs Locale - Day of Week Config for US

I'm using Momentjs in several places in an App I'm working on, and it is working fine. However, I am using endOf('week') and it is resulting in Saturdays. What I am expecting is Sundays.
I've been trying to find information about it, but it is not standing out anywhere how to get around it without changing code everywhere it is used. From my digging around, Moment automatically applies locales. And from looking in one of the locale files, I can see where this day of week (dow) is configured.
Example: moment/locale/en-gb.js
week : {
dow : 1, // Monday is the first day of the week.
doy : 4 // The week that contains Jan 4th is the first week of the year.
}
So what I gather is if I were in GB, end of week would be Sundays. But what I'm not clear on, is where are the defaults for U.S.? There is no en-US.js in the locale directory and it seems to default to dow:0
What I'm trying to determine is:
Is there a reason there's no en-US?
Is my best solution to just copy the en-gb.js as en-US.js? If I do that and include it in my App, will this affect others in other locales?
I did tinker in moment.min.js and changed dow from 0 to 1, and that resulted in Sundays being the end of week throughout the App. But that's not something I want to do.
Hopefully this is not just me missing the answers somewhere.
Any suggestions are welcome.
Thank you.
Update 1
Using:
momentjs 2.9.0
angular-moment 0.10.3
The week system in moment is not the easiest thing to understand. That said, the default 'en' is kind of understood to be the same as 'en-us'. If you want to update day of week to be Sunday, you can use locale customization. Ensure that you are using version 2.12 or greater, as that is when the updateLocale feature was introduced. Then simply run:
moment.updateLocale('en', {week : {
dow : 1, // Monday is the first day of the week.
doy : 4 // The week that contains Jan 4th is the first week of the year.
}});
This results in:
moment().endOf('week').format()
"2016-07-03T23:59:59-05:00"
If you are using 2.8 to 2.11, the syntax is simply:
moment.locale('en', {week : {
dow : 1, // Monday is the first day of the week.
doy : 4 // The week that contains Jan 4th is the first week of the year.
}});
So, not terribly different, but a bit ambiguous.

Resources