Need some help please. I am trying to group my click stats by week using underscore and moment.
here is the code:
var groupedByWeekFbCompleted = _.groupBy($scope.facebookObjects, function(item) {
return moment(item.timeclicked,"YYYY-MM-DD").isoWeek();
});
here is a screen shot of the chart it gives me
When I print groupedByWeekFbCompleted to the console this is what I get
The data comes from the DB formatted like this
2016-06-18 14:03:56
I can not figure out what 24, 26, 27, 28 and 29 represent. My hope is to display the first day of the week as a label and then group by that week
If you will take that date string and parse it using moment you will get something like
moment("2016-11-08 14:03:56", "YYYY-MM-DD HH:mm:ss").isoWeek()
// 45
24, 26, 27, 28 and 29 are the week day, and the values are arrays of dates within that week
EDIT
To display a date string on your labels, you can use the .format method to parse the timestamp you get from the moment function, provide it the same format you used, like so:
moment("2016-11-08 14:03:56", "YYYY-MM-DD HH:mm:ss").format("YYYY-MM-DD HH:mm:ss")
// output: "2016-11-08 14:03:56"
Related
I need to convert a date initially given as a string in the format "dd/mm/yyyy" to a valid date object but I'm running into problems. I use the moment.js library for this but when I try to convert it to a date object, it treats it internally incorrectly.
Initially I have:
var date_text = '02/01/2020 00:10'; //i.e. January 2, 2020.
var initial_date = new Date(date_text); //here js takes its default format and the problem starts.
var a = moment(initial_date,'DD/MM/YYYY');
console.log(a); //it keeps telling me that the date is February 1, 2020.
I have seen that this is often done "manually", i.e. by changing the order of the month and day. However, I find it hard to believe that a library as comprehensive and powerfull as moment.js has no way of doing this. I guess I haven't figured out how to do it.
Please, can someone help me in this regard?
What I specifically need is to pick up the date correctly (January 2nd and not February 1st) and preferably do it without having to alter the date "manually", that is, doing it only with the Date object of js and moment.js.
Thank you very much.
You don't really need initial_date to format the date_text when you are using moment.js Try the below code to fix the date parse issue
var date_text = '02/01/2020 00:10'; //i.e. January 2, 2020.
var a = moment(date_text, 'DD-MM-YYYY HH:mm');
console.log(a); // Thu Jan 02 2020 00:10:00 GMT+0530
Then format the date in your desired format
var b = a.format('DD/MM/YYYY');
console.log(b); // 02/01/2020
I am currently making a series of REST calls to a backend API and I have no control over the format of the date being sent back in the JSON.
The format that is being sent is this
Wed, 21 Nov 2018 03:00:00.000Z
IE11 considers this an invalid date. I have been using moment.js to get the current date and time and comparing it to the date and time being sent in the API. It is working perfectly everywhere except in IE. I have been trying everything I can from the Moment docs but everything that I return is considered invalid by IE11.
I am setting my date as follows
var date = new Date("Wed, 21 Nov 2018 03:00:00.000Z");
Update: I have also tried setting the date using moment
var date = "Wed, 21 Nov 2018 03:00:00.000Z"
date = moment(d, "YYYY-MM-DD HH:mm:ss").toDate();
I have tried many different formats and everything returns invalid.
This is what returns as Invalid according to IE. I have tried converting the date to a moment object first and then into a valid date format but that has not seemed to work either.
I was able to conclude that IE does not like the .000Z at the end of the date. It works if I cut that off but that all my times are in GMT.
The format YYYY-MM-DD HH:mm:ss you're putting in your momentJS constructor bears no resemblance to the date string you're actually inputting...you're telling moment to expect something like "2018-11-16 17:10:02". Maybe you've confused this with the format you want to output later, I'm not sure, because it clearly doesn't even come close to matching the example data.
Check http://momentjs.com/docs/#/parsing/string-format/ and choose the appropriate tokens to match the date format you're providing. Here's an example which will work for the date given in the example:
var d = "Wed, 21 Nov 2018 03:00:00.000Z";
var m = moment(d, "ddd, DD MMM YYYY HH:mm:ss.SZ"); //parse the date based on the format tokens given
console.log(m.format("YYYY-MM-DD HH:mm:ss")); //output the date in a different format
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
I am a newbie in javascript, come to my question.
I am using ionic 2 build application in which i am using date time picker for taking a time.
I am getting a a time format is in the "hh:mm" using time picker. eg(10:11) which is in string format and i am using Date() function which give me date is in something like
"Mon Aug 01 2016 01:32:03 GMT-0700 (Pacific Daylight Time)"
I want to replace "hh:mm"(01:32) from Date object with my string output of "hh:mm"(10:11) and then want to convert that into new Date object.
I also tried split and slice function but doesn't work.
If i get some guide about this will be very helpful.
I will be very helpful to all of you.
thanks
First of all, read up on Date.
In your case, the following code is a starting point.
var d = new Date();
d.setHours('10');
d.setMinutes('11');
Of course, you should exchange '10' and '11' with your picker data. Also, there are many other methods you can use on the Date object.
Given today is October 2015, in a calendar view, the September 27 - 29 appear on the calendar and Oct 31 appear. I'd like to get the start date that is appearing on the calendar AND the end date, so in this case, it'd be 9/27/2015 and 10/31/2015.
I'm using moment.js to try and calculate this. My thought is I'd get the calendar week and then convert that to find the start date and then get the end week the calendar is showing and convert that to get the last date shown. Its proving to be quite challenging which makes me thinking there is an easier way.
I'm looking at the documentation found here: http://momentjs.com/docs/#/get-set/ but I'm having trouble chaining some of the events together.
I have this to get me the month of the year:
var day = '2015-10-19';
var getStartWeekYear = moment(day).startOf("month").week();
var getEndDateWeekYear = moment(day).endOf("month").week();
and this is returning the expected value of 40 and 44 for the given date (day) but what would the next step be?
This is easier than you think. :)
var day = '2015-10-19';
var start = moment(day).startOf('month').startOf('week').format('YYYY-MM-DD');
var end = moment(day).endOf('month').endOf('week').format('YYYY-MM-DD');
console.log(start); // "2015-09-27"
console.log(end); // "2015-10-31"
I have set of docs in my couchdb here I mentioned one sample doc:
{
"_id": "26",
"_rev": "1-53ac67e9ec4b4ce8ffa9cd609e107aaf",
"customer_name": "Vadilal",
"type": "trip",
"duration": "10 hours 27 mins",
"end_time": "Jan 1, 2014 10:11:00 PM",
"start_time": "Jan 11, 2014 8:46:00 AM",
}
If I pass timestamp(key) from the URL, if it is in between start_time and end_time then i want to get the docs.
Example:
suppose url would be like this
.../trip/_design/trip/_view/trip?key="Jan 10, 2014 8:46:00 AM"
Here I am passing timestamp as Jan 10, 2014 8:46:00 AM so it comes in between start_time and end_time of above mentioned doc, in this case I need fetch the remaining information.
Kindly help to figure out this problem, it would be great help to me.
I have written function like below :
function(doc){
if(doc.type=="trip"){
var startTime=new Date(doc.start_time);
var endTime=new Date(doc.end_time);
emit([startTime.getTime(),endTime.getTime()], doc);
}
And calling URL as below :
../trip/_design/trip/_view/trip?startkey=[1390086890000]&endkey=[1390086890000,{}]
is above one correct according to my requirement???
That is unfortunately not possible. There are two problems with you requirement.
Views are arranged by a key
You could write a view like this to index documents by the start key or the end key respectively.
function(doc){
emit(doc.start_time, doc_id); // amend with end key to index by end key.
}
you can use three parameters in your query:
key - matches an exact key
startkey - matches all documents that are greater or equal to the start key
endkey - matches all documents that are greater or equal to the end key
The sorting problem
Depending on the type your index keys will be sorted either alphabetically or as integers.
So if your view trip is using a start key like this:
.../trip/_design/trip/_view/trip?start_key="Jan 10, 2014 8:46:00 AM"
You could return all values greater or equal to the string "Jan 10, 2014..." which would be true for "Jan 11, 2014" or "Jan 31, 2014" but there would be false positives for "July 22, 2011" or false negatives for "Feb 01, 2014" as F < J.
To solve this issue you will have to convert the start date to something that can be sorted in time order eg:
parse the date to date time and then convert to epoch like How to get time in milliseconds since the unix epoch in Javascript?
convert the date to yyyyMMddHHmmss formate like 20140110084600
Both of these will sort correctly.
Can you solve your problem
Yes, but with a little client side code. Here's the recipe:
create two views trip/by_start_date and trip/by_end_date which return the start and end date as keys respectively. In a way that is sorted as per the second section above.
Get two sets of documents.
Set 1 should return all documents that start before your date: ..._view/by_start_date?endkey=[your_date]
set 2 should return all documents that end after your date: ..._view/by_end_date?startkey=[your_date]
You then have two sets of document ids and the result you were after will be those documents that are in both these sets.
Further optimization
In the above solution you might just get too many values returned to handle. You can further cut it down using CouchDB's ability to key on more than one value. Key by both start and end date like this:
function(doc){
var start_time = some_conversion_function(doc.start_time);
var end_time = some_conversion_function(doc.end_time);
emit([start_time, end_time], doc_id);
}
If you have any more details on the maximum difference between start time and end time you can use this to further reduce the documents in each set. The below example will return the documents that start no later than X days before date and no later than date and end no later than X days after date.
..._view/by_start_date?startkey=[date-X,]&endkey=[date, date+X]
You can apply a similar logic to the by_end_date view.