I use Google Analytics, and the first thing I want to check every day is the current day's statistics. But there's no way to bookmark the current day.
The URL for any given day is: https://www.google.com/analytics/reporting/dashboard?id=XXXXXXX&pdr=20110921-20110921
You can see a date range at the end of the URL. 20110921 meaning 9-21, 2011.
How can I write a little javascript bookmarklet for Firefox to change the URL to the current date depending on what day I click on it?
Try this - it uses a Date object to get the date:
var date = new Date();
var str = "";
str += date.getFullYear();
str += pad2(date.getMonth() + 1);
str += pad2(date.getDate());
function pad2(n) {
var str = String(n);
if(str.length < 2)
str = "0" + str;
return str;
}
location.href = "https://www.google.com/analytics/reporting/dashboard?id=XXXXXXX&pdr="+str+"-"+str;
Bookmarklet:
javascript:(function(){function d(a){a=String(a);a.length<2&&(a="0"+a);return a}var c=new Date,b="";b+=c.getFullYear();b+=d(c.getMonth()+1);b+=d(c.getDate());location.href="https://www.google.com/analytics/reporting/dashboard?id=XXXXXXX&pdr="+b+"-"+b})();
JavaScript has date methods that can individually give the parts of a date, but .toISOString() might perhaps work for your application most concisely. Very little string manipulation would have to be performed on the result to get the UTC date in the correct format. For example:
javascript:
d = new Date().toISOString().slice(0, 10).replace(/-/g, '');
location = "https://www.google.com/analytics/reporting/dashboard?id=XXXXXXX&pdr="
+ d + "-" + d;
To build on the answer by #PleaseStand - Firefox even has a non-standard Date.toDateLocale() function. So the whole thing can be simplified even further:
javascript:void(location.href = "https://www.google.com/analytics/reporting/dashboard?id=XXXXXXX&pdr=" +
new Date().toLocaleFormat("%Y%m%d-%Y%m%d"))
Customize this code below with your Google Analytics ID, timezone offset, and other desired parameters.
Use a site like this to convert JavaScript to a bookmarklet.
Rejoice.
var gaID = YOUR_ID;
var hourOffset = 8;
var rowCount = 50;
var utcDate = new Date(new Date().toUTCString());
utcDate.setHours(utcDate.getHours() - hourOffset);
var localDate = new Date(utcDate);
var todayDate = localDate.toISOString().split("T")[0].replace(/-/g, "");
location.href = "https://analytics.google.com/analytics/web/#/report/advertising-adwords-keywords/" + gaID +"/_u.date00=" + todayDate + "&_u.date01=" + todayDate + "&explorer-table.plotKeys=%5B%5D&explorer-table.rowCount=" + rowCount;
I know this is a really old thread, but it is still relevant.
I found that if you set up your report using todays date, then edit the URL and use a future date, it will default to the current date's statistics.
For example, I use this:
https://analytics.google.com/analytics/web/#/dashboard/xxxxxxxxxxxxxxxx/xxxxxxxxxxxxxxxx/_u.date00=20221130&_u.date01=20221130/
The date codes are both the same date 2 years in the future. This causes analytics to use show the current dates stats.
Related
I am trying to send a request to node server. The browser sends the date in mm/dd/yyyy format which is handled by server in the below manner.
var endDate;
if (req.query.endDate) {
endDate = new Date(req.query.endDate);
}
This works just fine in chrome and other browsers except IE.
In IE11, it encodes the date to '?5?/?24?/?2017' from '5/24/2017' for some reason.
To fix this I am trying to do this :
var endDate;
if (req.query.endDate) {
endDate=req.query.endDate.toString().trim();
endDate=endDate.toString().split('?').join('');
console.log('Log',endDate);
endDate = new Date(endDate);
}
Expected result is '5/24/2017' But it does not work.
When i see the split('?') for '?5?/?24?/?2017' in the logs it shows ['?5?/?24?/?2017'] as the result. Why is it not splitting the string?
Am I doing Anything wrong?
Node version : 4.3.2(using NVM)
In your case "?" could be not the question mark, assume, some UTF-8 symbol.
It can be used the date formatting itself:
var endDate = new Date(req.query.endDate);
endDate.toLocaleDateString()
or
var endDate = new Date(req.query.endDate);
(endDate.getMonth() + 1) + "/" + endDate.getDate() + "/" + endDate.getFullYear();
or regexp approach:
req.query.endDate.toString().match(/[0-9]+/g); // [month, date, year]
I need to get a date in this format:
2016-07-06T10:57Z
Using this code I have been able to get a date in a format somewhat like I need:
var isoDate = new Date().toISOString();
2016-07-06T08:46:08.127Z
But is there a way I can remove the seconds and fraction of seconds from the date so it appears exactly like the date: "2016-07-06T10:57Z" ?
You will always want to remove the last 8 characters ('Z' included) thus you can use a function like slice
isoDate = isoDate.slice(0, -8); //Remove seconds + fractions + Z
isoDate += "Z"; //Add back the Z
You can use this way because the format returned by toISOString() will always be
YYYY-MM-DDTHH:mm:ss.sssZ
Please try
var isoDate = new Date().toISOString();
var pos = isoDate.lastIndexOf(':');
var datePart1 = isoDate.substring(0,pos);
var datePart2 = isoDate.substr(-1, 1);
var dateStr = datePart1+datePart2;
console.log(dateStr);
I am trying to convert a date string into a date object within javascript. My date has the following format:
"13.02.2015 12:55"
My current approach was:
var d = new Date("13.02.2015 12:55");
But this didnt work and always returns invalid date. If I enter a date as "12.02.2015 12:55" it works in chrome but not in firefox.
I guess this is because he thinks the first part is the month, but in germany this is not the case.
How can I get this to work?
use moment.js:
var date = moment("13.02.2015 12:55", "DD.MM.YYYY HH.mm").toDate();
Update 2022-05-28:
Meanwhile the project status of moment.js has changed. Therefore I strongly suggest to read https://momentjs.com/docs/#/-project-status/ and observe the recommendations.
try the ISO 8601 format,
or better yet, read this http://www.ecma-international.org/ecma-262/5.1/#sec-15.9
Edit: if you have no other choice than to get it in that format though, i guess you'll need something like this:
function DDMMYYYY_HHMMtoYYYYMMDD_HHMM($DDMMYYYY_HHMM) {
var $ret = '';
var $foo = $DDMMYYYY_HHMM.split('.');
var $DD = $foo[0];
var $MM = $foo[1];
var $YYYY = $foo[2].split(' ') [0].trim();
var $HH = $foo[2].split(' ') [1].split(':') [0].trim();
var $MMM = $foo[2].split(' ') [1].split(':') [1].trim();
return $YYYY + '-' + $MM + '-' + $DD + ' ' + $HH + ':' + $MMM;
}
var d=new Date(DDMMYYYY_HHMMtoYYYYMMDD_HHMM('13.02.2015 12:55'));
I've got a problem with date object in IE8, and some older browsers. On website I have input hidden, where I keep date, and after change new date should be in that field.
On my machine everything is fine, but on some others I get NaN-NaN-NaN, that's my code:
var date = new Date($('#curDate').val());
//date.setDate(date.getDate() - 7);
var dateMsg = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
alert(dateMsg);
When I run this file (php), in hidden input I've got Monday's date from the current week 2013-03-25.
This alert return me NaN-N.. on Win XP IE8, and on very old mac, I recon it's problem with object. How to take date value and convert it to object in javascript?
Never use new Date(some_string) - it's unreliable because it depends on the user's locale.
Break the string into its yy/mm/dd components yourself and then call new Date(y, m - 1, d)
Problem with your hyphens..
Convert your hyphens('-') with slashes('/')
var dateStr=$('#curDate').val();
var a=dateStr.split(" ");
var d=a[0].split("-");
var t=a[1].split(":");
var date = new Date(d[0],(d[1]-1),d[2],t[0],t[1],t[2]);
or
var date=new Date(convertToSlash($('#curDate').val()));
function convertToSlash(string){
var response = string.replace(/-/g,"/");
return response;
}
You can also use new Date(some_string) format. It's reliable. However, the datestring must be in ISO format that is yyyy/mm/dd.
I want to pass back a variable from a promt box to the date string. So if a person adds 5 days the date will bump up 5 days. I am new to javascript and this is my first test script any resources you could list in your answer I will read up on.
<html>
<head>
<script type="text/javascript">
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var dateString = "Today's Date " + month + "/" + day + "/" + year;
function loadDate(){
document.getElementById('dateSpan').innerHTML = dateString;
}
</script>
</head>
<body onload='loadDate()'>
<form name=myform>
<span id='dateSpan'></span><input type=button value="add days" onclick="var name=prompt('How many days do you want to add?','5 or 6')"/>
</form>
</body>
Something like the following should help. Note that it doesn't do any validation or checking (is the date string correct in the element? did the user enter a number?) so you'll need to add all that and deal with errors.
function updateDate(el) {
// get the text of the element
var text = el.innerHTML;
// Convert to a date object
var b = text.split('-');
var date = new Date(b[0], b[1] - 1, b[2]);
// Ask for days to add
var toAdd = prompt('How many days to add?');
// Adjust date
date.setDate(date.getDate() + Number(toAdd));
// Write date to page
el.innerHTML = date.getFullYear() + '-' +
addZ(date.getMonth() + 1) + '-' +
addZ(date.getDate());
// Helper just for this function
function addZ(n) {
return (n < 10? '0' : '') + n;
}
}
And some related HTML:
<span id="dateSpan" onclick="updateDate(this)">2012-05-22</span>
Note also that innerHTML gets all the HTML content too, but for something like this it shoudl be fine. If you need just the text, then use either textContent or innerText based on a feature test for which is supported.
Edit
Added a snipped of HTML to make it work. Note that the date is an ISO8601 short form: year-month-day.
I'd go with using the date.js lib.
This makes dates a lot easier to work with and it's well documented too.
http://www.datejs.com/
Then once you have the number of days to add you can easily bump the date up.
//Assuming Xdays is a var with your number of days.
var today = Date.today();
var past = Date.today().add(-Xdays).days();
var future = Date.today().add(Xdays).days();
//format to a string
Date.today().toString("d-MMM-yyyy"); // 19-Nov-2007