I have an array of different times, and I need to order from low to high but directly from the driver, I'm dealing with the following:
var arrayList =[{hora: '8:30'},{hora: '14:30'},{hora:'22:30'},{hora:'07:00'}]
$scope.array =$filter('orderBy')(arrayList, true);
the result is not as expected, that I'm wrong
I'm not proficient with angular, but usually, the problem sorting time variables is that they are stored as strings and not exactly referenced as time variables.
On this cases I usually split them in hours and minutes, multiply hours by 60 and add minutes, then sort for the resulting integer value.
Related
I would like to try and convert time duration strings to a moment.js duration object. An example of the string format is as follows: "1h30m", which should correspond to 1 hour, 30 minutes, and 0 seconds.
My first thought was to use regex so that I could pull the hours, minutes and seconds but I have a feeling that there's a more efficient way to handle it - the end goal is to use these to calculate how long until a command is run - I saw there was a library called momentjs that I feel could possibly handle this, but the docs don't give a clear way on handling duration formatting in the format that I have in mind. I can provide the code I have written so far, though I don't imagine it would be of much help.
Yes, you can use moment.js to do this - but I'm not sure if it's strictly necessary as it can also be accomplished through simple string manipulation.
String Manipulation Approach:
function parseTimeSpan(timeString) {
let parts = timeString.split("h");
return {
hours: Number(parts[0]),
minutes: Number(parts[1].slice(0, -1))
};
}
JsFiddle Here
Note this will only work with strings that contain both the hour and minute component, and does not support seconds.
Moment.JS Approach:
function parseTimeSpan(timeString) {
return moment.duration("PT" + timeString.toUpperCase());
}
JsFiddle Here
This approach is more robust and handles far more use cases, but is slower and requires an external library.
I am using bs-datepicker for save time as number in mongodb. In mongodb, it has time field with value i.e, 38700000 and 41400000 which are 16.15 and 17.00 respectively.
Now, i want to format the above time in readable form. I am using moment 2.16. Here i try but unable to do jsfiddle
console.log(moment.unix(38700000).format('HH:mm')) //03:30, expect:16:15
console.log(moment.unix(41400000).format('HH:mm')) //09:30, expect:17:00
And, how to convert from HH:mm to number. e.g 18:71 convert into number
The data you have is in terms of milliseconds since 1970-01-01 00:00:00.000 UTC.
38700000 == 1970-01-01T10:45:00Z
41400000 == 1970-01-01T11:30:00Z
If you convert those to the time zone offset you mentioned in comments (UTC+05:30) then the time portion of the values line up to what you expected.
38700000 == 1970-01-01T10:45:00Z == 1970-01-01T16:15:00+05:30
41400000 == 1970-01-01T11:30:00Z == 1970-01-01T17:00:00+05:30
Your values don't align at all, because you are using moment's unix function, which expects time in seconds, not milliseconds (because Unix Time is in whole seconds unless otherwise specified).
Also, by relying on the local time zone, you may get different values than expected if the user is in a different time zone.
The correct way to get what you asked for is:
moment(38700000).utcOffset("+05:30").format("HH:mm") // "16:15"
moment(41400000).utcOffset("+05:30").format("HH:mm") // "17:00"
Do recognize though that this means your original data is not stored with reference to UTC, but with reference to UTC+05:30. It would be better if you had stored UTC-based values instead, so you would do:
moment.utc(58500000).format("HH:mm") // "16:15"
moment.utc(61200000).format("HH:mm") // "17:00"
Of course, the real problem here is that you're storing a time-of-day into a date-time field. Probably the best thing to do would to not use the Date type at all in your MongoDB data, but rather just store the string "16:15" or the equivalent number of total minutes as an integer (60 * 16 + 15 == 975).
Given
var fromDatetime = "10/21/2014 08:00:00";
var toDatetime = "10/21/2014 07:59:59";
Target
Need to compare this two given datetime to check if this is a valid datetime inputs.
Im thinking that I could use this solution. .
var fromDatetime = new date("10/21/2014 08:00:00");
var toDatetime = new date("10/21/2014 07:59:59");
then compare each segment
fromDatetime.getFullYear() to toDatetime.getFullYear(),
fromDatetime.getMonth() to toDatetime.getMonth(),
so on so forth until I get a non equal comparison, then return.
My question is. . Is that the best case in comparing datetime in Javascript?
Any suggested solutions other than this will be much appreciated. .
Thank you in advance.
If you only need to know if one date is before or after another, you can use normal comparison operators:
if (dateA > dateB) { ... }
(Note that to instantiate Date objects, it is new Date(myDateString), not new date() as in your example.)
This works because the Date objects will be coerced to the underlying epoch values, which are just numbers representing the count of milliseconds that have passed since Jan 1, 1970, GMT. (Relational comparison uses the result of the operands’ ##toPrimitive or valueOf() functions if available.) As a result, this will be sensitive down to the millisecond, which may or may not be desired. You can control the granularity of the comparison either by making sure the input only includes values up to the hour/minute/whatever OR by doing piecemeal comparisons like you described in your question.
For more advanced date comparison operations, I highly recommend using a library like Moment.js, because natively there is not much to work with. For example Moment provides methods like
moment(myDateString).startOf('hour')
which can be very helpful if you need to make comparisons only at a particular level of specificity. But none of the datetime libraries are terribly lightweight, so only use them if you are either doing serverside code or you expect to rely on them extensively. If you only need to do this one comparison, it will be wiser to roll your own solution.
I'm trying to visualize some data I have. Here is the fiddle: http://jsfiddle.net/hohenheim/6R7mu/10/
For this visualization, I started a small subset of the JSON data I'm trying to visualize, data2 which looks like
data2 = [
{
"startDate":1396263600.0,
"adId":2483231759355,
"endDate":1401101940.0,
"impressions":754831
},
{
"startDate":1393851600.0,
"adId":2750329551133,
"endDate":1404212340.0,
"impressions":3947368
}
];
Notice there is a date range in the data. My goal is to split the impressions uniformly into all the days in that date range and aggregate all the impressions per day over all ads. IE the final result will only have 2 attributes: date and impressions. I have a solution that SHOULD work but days are not aggregating properly because the millisecond representations of the days are not equal even though they may approximate to the same day. For example, 1398164400000 and 1398171600000 represent the same day but have different millisecond values.
Basically, if you examine the middle part of the region on the chart, notice there are oscillating values but really each pair of differing values should actually represent just 1 day summed together.
Is there any way to properly merge 2 millisecond versions of dates that approximate to the same day?
Most clocks that are used to produce timestamps don't include leap-seconds, so you can isolate the day part by subtracting
(t % (24 /* hours per day */
* 3600 /* seconds per hour */
* 1000 /* milliseconds per second */ ))
from the time-stamp t.
Even if the timestamp does include leap-seconds, that function will still give you a pretty good proxy for day-of comparison. It just may get dodgy with timestamps that are close to midnight.
If you are in a time-zone that is far from the day boundary, you will probably want to adjust your timestamp to millis since the epoch in the local timezone, and just ignore DST which again will introduce some inaccuracy around midnight.
Math isn't really my thing, but what I'm trying to figure out is how to predict/estimate the next number from a dataset.
Let's say I have an array:
var values = new Array(1,4,3,5,6,10,4,15);
Does anyone know a formula in javascript that could guess the next number after 15 based off the previous values in the array.
Basically I have an array of total numbers from daily sales, each item in the array is the total for a single day. So I'd like something that could guess what tomorrow's sale might be.
Based on the data you're providing, it seems you can only predict what tomorrow's sale might be by taking the average of your dataset.
If you had additional data, say, day of the week, you could take the average of all sales on Tuesdays, and then make a prediction based off of that average.
Have a look at the various moving average methods - you can choose whichever suits your application best.