I'm trying to change the date format for some data coming in. I can change it to this format "02-10" but i wanted it to look like this "Feb 10 2015"
So far i have something like this that changes it to each day of the week, but i would like to change that. Here's what i have.
$scope.hourlyData = [];
var hourData = [];
var hourItem = [];
var dailyData = [];
var day;
var date;
var Everymonth = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var item = ['Day', 'Number of Interactions', {role: 'style'}];
dailyData.push(item);
var dailyReports = $scope.tweetReports.dailyReports;
$scope.numberOfdailyReports = dailyReports.length;
for (var i = 0; i < dailyReports.length; i++) {
if (dailyReports[i] != null) {
//console.log("Tweets on " + dailyReports[i].day + ":" + dailyReports[i].tweets);
day = dailyReports[i].day.split(" ")[0];
date = dailyReports[i].day.split(" ")[1];
date = date.substr(date.indexOf('-') + 1);
if (numDays =< 7) {
day = day.toLowerCase();
day = day.charAt(0).toUpperCase() + day.substr(1);
} else {
day = date;
}
If you are open to use libraries, You can use momentJS like this
jsfiddle
var dateString = "2013-07-18";
var date = moment(dateString).format('MMM DD YYYY');
Related
I am trying to write an apps script and assign it to a button. When the button is pressed, it will activate a function I named clockin(). What this function does is to look for today's date on column B and write the current time on column C. The problem is this code is not writing any value on the defined cell which kinda sucks. I'm new to Javascript, hence requiring your assistance. My code is below:
function todayDateNowTime () {
const monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var today = new Date()
var month = monthNames[today.getMonth()]; //months from 1-12
var day = today.getDate();
var year = today.getFullYear();
var seconds = today.getSeconds();
var minutes = today.getMinutes();
var hour = today.getHours();
var todayDate = day+"-"+month+"-"+year;
var nowTime = hour+":"+minutes+":"+seconds;
console.log(todayDate);
console.log(nowTime);
return todayDate, nowTime;
}
function clockin(todayDate, nowTime) {
todayDate, nowTime = todayDateNowTime();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = sheet.getDataRange().getValues();
for(var i = 0; i<data.length;i++){
if(data[i][1] == todayDate) { //[1] because column B
var range = SpreadsheetApp.getActiveSpreadsheet().getActiveCell("C"+i)
range.setValue(nowTime);
}
}
}
I have made my gsheet publically available to view right here.
I have also included a screenshot here if it helps:
I made some small adjustments to your code. See if it now works?
function todayDateNowTime () {
const monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct", "Nov", "Dec"];
var today = new Date()
var month = monthNames[today.getMonth()]; //months from 1-12
var day = today.getDate();
day = day < 10 ? "0" + day : day;
var year = today.getFullYear();
var seconds = today.getSeconds();
var minutes = today.getMinutes();
var hour = today.getHours();
var todayDate = day+"-"+month+"-"+year;
var nowTime = hour+":"+minutes+":"+seconds;
return [todayDate, nowTime];
}
function clockin() {
var dateAndTime = todayDateNowTime();
var todayDate = dateAndTime[0];
var nowTime = dateAndTime[1];
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = sheet.getDataRange().getDisplayValues();
for(var i = 0; i<data.length;i++){
if(data[i][1] == todayDate) { //[1] because column B
var range = sheet.getRange(i+1, 3)
range.setValue(nowTime);
}
}
}
My function displays the current date along with the next 60 days however I want the current date to be highlighted. What would be the best approach?
var date = new Date();
var dayInt = date.getDate();
var month = date.getMonth();
var year = date.getFullYear();
var dateRange = document.getElementById('calendar-table-range');
var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
document.getElementById("month").innerHTML = monthNames[month];
document.getElementById("year").innerHTML = year;
for(var day = 0; day < 60; day++) {
var date = new Date();
date.setDate(date.getDate() + day);
var cell = document.createElement("li");
var cellText = document.createTextNode(day);
var date = ('0' + date.getDate()).slice(-2) + ' '
+ monthNames[date.getMonth()] + ' '
// + date.getFullYear();
cell.innerHTML = date;
dateRange.appendChild(cell);
}
Since you're always showing the next 60 days, the current date is always the first date in the list, so it's easy to target with a CSS selector. For example:
#calendar-table-range li:first-child {
background-color: yellow;
}
Another way to go about it is to create a timestamp in the same format as the dates in the range then search for it.
E.g. the following, which puts formatting into a separate function and removes code that wasn't being used:
function formatDate(d){
let monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
return ('0 ' + d.getDate()).slice(-2) + ' '
+ monthNames[d.getMonth()];
}
function highlightToday() {
let today = formatDate(new Date());
let cells = document.querySelectorAll('#calendar-table-range > li');
for (var i=0, iLen=cells.length; i<iLen; i++) {
if (cells[i].textContent == today) {
cells[i].style.color = 'red';
return;
}
}
}
var date = new Date();
var dayInt = date.getDate();
var month = date.getMonth();
var year = date.getFullYear();
var dateRange = document.getElementById('calendar-table-range');
var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
document.getElementById("month").innerHTML = monthNames[month];
document.getElementById("year").innerHTML = year;
for(var day = 0; day < 6; day++) {
var date = new Date();
date.setDate(date.getDate() + day);
var cell = document.createElement("li");
var date = formatDate(date);
cell.innerHTML = date;
dateRange.appendChild(cell);
}
<div id="month"></div>
<div id="year"></div>
<input type="button" onclick="highlightToday()" value="highlight today">
<ol id="calendar-table-range">
</ol>
How to format the javascript Date object the way stackoverflow does it.
For example. Aug 23 '10 at 23:35
This is what I tried.
new Date(val.replace(' ','T')+'Z').toString().split('GMT')[0]
This works cross browser. But doesn't look neat.
function formatDate(date) {
var monthNames = [
"Jan", "Feb", "Mar",
"Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct",
"Nov", "Dec"
];
var day = date.getDate();
var monthIndex = date.getMonth();
var month = monthNames[monthIndex];
var year = date.getFullYear().toString().substring(2,3);
var hours = date.getHours();
var minutes = date.getMinutes();
return month+' '+day+" '"+year+' at '+hours+':'+minutes;
}
Try this:
var date = new Date();
var formattedDate =
(date.toLocaleString("en-us", { month: "long" })) + " " +
date.getDate() + " '" + (date.getFullYear() % 100);
var formattedTime = date.getHours() + ':' + date.getMinutes();
alert( formattedDate + " at " + formattedTime );
Here's a JSFiddle.
I have written a code, where it will show the next 12 months from current month. Suppose, now is Oct 2015, so it will show all next 12 months, that is upto Oct 2016. I am showing it in a list. But, I want to create a box with right-left arrow enabled. After clicking on left-right arrow it will show next item. After clicking on right arrow it will show next item and after clicking on left arrow it will show previous item. Data items will be shown in a box. Please check my below code, which will print next 12 months from current.
angular.module('picker', []).controller('pickercontroller', function($scope) {
var date = new Date();
var months = [],
monthNames = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
for(var i = 0; i <= 12; i++) {
months.push(monthNames[date.getMonth()] + ' ' + date.getFullYear());
date.setMonth(date.getMonth() + 1);
}
$scope.months = months;
});
<div ng-app="picker">
<div ng-controller="pickercontroller">
<li ng-repeat="currMonth in months">{{currMonth}}</li>
</div>
</div>
Also, check my fiddle :- http://jsfiddle.net/abhijitloco/cqbqow2L/
Check this code to see how you can change the selected month via ng-click
https://jsfiddle.net/jddg3som/
Controller
angular.module('picker', []).controller('pickerCtrl', function($scope) {
var date = new Date();
var months = [],
monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
];
for (var i = 0; i < 12; i++) {
months.push(monthNames[date.getMonth()] + ' ' + date.getFullYear());
date.setMonth(date.getMonth() + 1);
}
$scope.changeMonth = function(steps) {
if ($scope.monthIndex + steps >= 0 &&
$scope.monthIndex + steps <= 11
) {
$scope.monthIndex = $scope.monthIndex + steps;
$scope.monthName = $scope.months[$scope.monthIndex];
}
};
$scope.monthIndex = 0;
$scope.months = months;
$scope.monthName = months[0];
});
View
<div ng-app="picker">
<div ng-controller="pickerCtrl">
<h1> {{monthName}} </h1>
<button ng-click="changeMonth(1);">Next Month</button>
<button ng-click="changeMonth(-1);">Previous Month</button>
<li ng-repeat="currMonth in months">{{currMonth}}</li>
</div>
</div>
Use ng-click to handle button event and for next month check whether next month is available in months array or not. If available then set next month from array.
Same for previous month
angular.module('picker', []).controller('pickercontroller', function($scope) {
var date = new Date();
var months = [],
monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
];
for (var i = 0; i <= 12; i++) {
months.push(monthNames[date.getMonth()] + ' ' + date.getFullYear());
date.setMonth(date.getMonth() + 1);
}
$scope.months = months;
$scope.currMonth = months[0];
$scope.nextMonth = nextMonth;
$scope.prevMonth = prevMonth;
function prevMonth() {
var index = $scope.months.indexOf($scope.currMonth);
if (index != 0 && index < $scope.months.length) {
$scope.currMonth = $scope.months[index - 1];
}
}
function nextMonth() {
var index = $scope.months.indexOf($scope.currMonth);
if (index < $scope.months.length - 1) {
$scope.currMonth = $scope.months[index + 1];
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="picker">
<div ng-controller="pickercontroller">
<p>{{currMonth}}</p>
<button ng-click="prevMonth()">Previous Month</button>
<button ng-click="nextMonth()">Next Month</button>
</div>
</div>
What I am trying to achieve is when I input a date into this function, it should give me this:
If the date is:
Today - output as Today
Tomorrow - output as Tomorrow
Yesterday - output as Yesterday
Else - output as "Month - Day" format
But I can't get this to work. Any help? JsFiddle here.
//My Date variable
var mydate = "22-Nov-2012"
alert(MDFormat(mydate));
/*
If the date is:
Today - show as "Today";
Tomorrow - show as "Tomorrow"
Yesterday - show as "Yesterday"
Else - show in "Month - Day format"
*/
function MDFormat(MMDD) {
MMDD= new Date(MMDD);
var months = ["Jan", "Feb", "Mar", "Apr", "May", "June", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var day = MMDD.getDate();
var month = months[MMDD.getMonth()];
var currentDay = new Date().getDay();
var today;
if (currentDay == MMDD.getDay()) {
today = MMDD.getDay();
}
var tmr = currentDay + 1;
var yest = currentDay - 1;
var strDate;
switch (MMDD) {
case today:
strDate = "Today";
break;
case tmr:
strDate = "Tomorrow";
break;
case yest:
strDate = "Yesterday";
break;
default:
strDate = month + "-" + day;
break;
}
return strDate;
}
Note: I prefer a plugin-less way.
getDate only works on valid predefined date formats. The one you are using is not a valid predefined format. If you try this:
var mydate = "Nov 22, 2012";
alert(MDFormat(mydate));
it works.
Also took the liberty of fixing up a few problems with your logic:
//My Date variable
var mydate = "Nov 22, 2012"
alert(MDFormat(mydate));
/*
If the date is:
Today - show as "Today";
Tomorrow - show as "Tomorrow"
Yesterday - show as "Yesterday"
Else - show in "Month - Day format"
*/
function MDFormat(MMDD) {
MMDD = new Date(MMDD);
var months = ["Jan", "Feb", "Mar", "Apr", "May", "June", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var strDate = "";
var today = new Date();
today.setHours(0, 0, 0, 0);
var yesterday = new Date();
yesterday.setHours(0, 0, 0, 0);
yesterday.setDate(yesterday.getDate() - 1);
var tomorrow = new Date();
tomorrow.setHours(0, 0, 0, 0);
tomorrow.setDate(tomorrow.getDate() + 1);
console.log(MMDD.getTime(),today.getTime(),MMDD.getTime()==today.getTime());
if (today.getTime() == MMDD.getTime()) {
strDate = "Today";
} else if (yesterday.getTime() == MMDD.getTime()) {
strDate = "Yesterday";
} else if (tomorrow.getTime() == MMDD.getTime()) {
strDate = "Tomorrow";
} else {
strDate = months[MMDD.getMonth()] + "-" + MMDD.getDate();
}
return strDate;
}
Demonstration: http://jsfiddle.net/xqnc8/4/
Try it with Datejs plugin - it's easy to work with. I wired up one possible solution for you, http://jsbin.com/otasiz/1/edit. Also below:
var downTheRoad = "11/30/2012";
var yesterday = "11/20/2012";
var tomorrow = "11/22/2012";
var today = "11/21/2012";
test(downTheRoad);
test(yesterday);
test(tomorrow);
test(today);
function test(dateStr) {
var date = Date.parse(dateStr);
var today = Date.parse("today");
var todayStr = today.toString("MM/dd/yyyy");
var tomorrow = Date.parse("tomorrow");
var tomorrowStr = tomorrow.toString("MM/dd/yyyy");
var yesterday = Date.parse("yesterday");
var yesterdayStr = yesterday.toString("MM/dd/yyyy");
if (dateStr === todayStr) {
alert(dateStr + " as today");
} else if (dateStr === tomorrowStr) {
alert(dateStr + " as tomorrow");
} else if (dateStr === yesterdayStr) {
alert(dateStr + " as yesterday");
}
else {
alert(dateStr + " as " + Date.parse(dateStr).toString("MMM-dd"));
}
}