Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Adding weekdate

Hi, all! I am building a Versa Clockface, I can put month, date and time on it, but still have problem with adding weekdate( Like Mon, Tue.....). Is there anyone can help me with this?Thanks!

Best Answer
0 Votes
21 REPLIES 21

Hi sspyli,

you can use getDay():

let weekday = date_var.getDay();
if(weekday === 0) {
console.log("Sun");
} else if(weekday === 1) {
console.log("Mon");
} else if(weekday === 2) {
console.log("Tue");
} else if(weekday === 3) {
console.log("Wed");
} else if(weekday === 4) {
console.log("Thu");
} else if(weekday === 5) {
console.log("Fri");
} else if(weekday === 6) {
console.log("Sat");
} else {
console.log("Err");
}
Best Answer
0 Votes

Thank you SO much! 

 

I tried to use those, but  I keep getting an error message which says "date_var is not defined weekday". How would I go about doing that?

Best Answer
0 Votes

date_var is simply the variable name nowo chose to use to make that code snippet (it's not a special keyword, or anything) - you will need to change that to whatever you currently call your date variable.

Best Answer
0 Votes

Then I changed the name, shows "Expected  a function", looks like still missing some code here.

Best Answer
0 Votes

Post the code you're using - that's the only way we will be able to help more.

Best Answer
0 Votes

import clock from "clock";
import document from "document";
import { preferences } from "user-settings";
import * as util from "../common/utils";
import { today } from "user-activity";
import { display } from "display";

console.log((today.local.steps || 0) + " steps");

let txtSteps = document.getElementById("txtSteps");


function update() {

steps.text = today.adjusted.steps || 0 + " steps";
}
// Update the clock every minute
clock.granularity = "minutes";


// Get a handle on the <text> element
const myLabel = document.getElementById("myLabel");
const myweekday = document.getElementById("myWeekday");
const myMonth = document.getElementById("myMonth");
const myDay = document.getElementById("myDay");
const txtSeps = document.getElementById("txtSteps");

// inside the clock tick handler
txtSteps.text = today.adjusted.steps || 0 + " steps";

// Update the <text> element every tick with the current time
clock.ontick = (evt) => {
let today = evt.date;
let hours = today.getHours();
let monthnum = today.getMonth();
let day = today.getDate();

var month = new Array();
month[0] = "Jan";
month[1] = "Feb";
month[2] = "Mar";
month[3] = "Apr";
month[4] = "May";
month[5] = "Jun";
month[6] = "Jul";
month[7] = "Aug";
month[8] = "Sep";
month[9] = "Oct";
month[10] = "Nov";
month[11] = "Dec";
let monthname = month[monthnum];

let weekday = date1.getDay();
if(weekday === 0) {
console.log("Sun");
} else if(weekday === 1) {
console.log("Mon");
} else if(weekday === 2) {
console.log("Tue");
} else if(weekday === 3) {
console.log("Wed");
} else if(weekday === 4) {
console.log("Thu");
} else if(weekday === 5) {
console.log("Fri");
} else if(weekday === 6) {
console.log("Sat");
} else {
console.log("Err");
}



display.onchange = function() {
if (display.on) {
console.log("ON");
updateSteps();
}
}



if (preferences.clockDisplay === "12h") {
// 12h format
hours = hours % 12 || 12;
} else {
// 24h format
hours = util.zeroPad(hours);
}
let mins = util.zeroPad(today.getMinutes());
myLabel.text = `${hours}:${mins}`;
myMonth.text = `${monthname}`;
myDay.text = `${day}`;
myWeekday.text = `${weekday}`;

}

Best Answer
0 Votes

Use the same setup as you use already for your month name.

 

Replace

 

let weekday = date1.getDay();
if(weekday === 0) {
console.log("Sun");
} else if(weekday === 1) {
console.log("Mon");
} else if(weekday === 2) {
console.log("Tue");
} else if(weekday === 3) {
console.log("Wed");
} else if(weekday === 4) {
console.log("Thu");
} else if(weekday === 5) {
console.log("Fri");
} else if(weekday === 6) {
console.log("Sat");
} else {
console.log("Err");
}

 

with

 

var day = new Array();
day[0] = "Mon";
day[1] = "Tue";
day[2] = "Wed";
day[3] = "Thu";
day[4] = "Fri";
day[5] = "Sat";
day[6] = "Sun";
let dayname = day[today.getDate];

 

then

myWeekday.text = `${weekday}`;

becomes

myWeekday.text = `${dayname}`;

Best Answer
0 Votes

Where is date1 declared and initialised?

Peter McLennan
Gondwana Software
Best Answer
0 Votes

I just changed that one, have no clues to declare. Smiley Sad



 

Best Answer
0 Votes

EDIT: Ignore. See my suggested fix above.

Best Answer
0 Votes

Before I added the weekday code,  the clock face I had built like this.

thumbnail.jpg

Best Answer
0 Votes

I've edited my answer above. Now that I can see your code, it's easier to replicate the monthname setup you're already using.

 

In your code above, you don't actually set a name for the day - you're logging the name to the console, but never actually retaining the name, so at best your weekday.text would be "0", "1", ... "6". In my code above, the name is actually set and used.

 

Note that for me the first day of the week is Monday, so I used that for value 0... If your week starts with Sunday, use that for value 0 (as nowo did in their example).

Best Answer
0 Votes

I replace all as you posted, but still shows my Weekday is not defined.

Best Answer
0 Votes

Check your code:

 

const myweekday = document.getElementById("myWeekday");

               ^ note not a capitol W

 

but later on you call it with a capitol:

 

myWeekday.text = `${dayname}`;

 

I suspect that's all you're seeing just now.

Best Answer
0 Votes

Thank you for that. Then I replaced the "const weekday" to "const Weekday". Then another issue came up, Unhandled TypeError, "can't set property "text"of null my Weekday.at the final line. And also did you see the clock face, all the weekday came out.

Best Answer
0 Votes

The clockface shows like this122.jpg

Best Answer
0 Votes

This is from the original Fitbit sample.  It has been working for me...

 

//Get the date in the proper order
function formatDate(date) { var monthNames = [ "Jan", "Feb", "Mar", "April", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec" ]; var dayNames = [ "Sun", "Mon", "Tues", "Weds", "Thurs", "Fri", "Sat" ]; var day = date.getDate(); var monthIndex = date.getMonth(); var year = date.getFullYear(); var todayIndex = date.getDay(); return dayNames[todayIndex] + ', ' + day + ' ' + monthNames[monthIndex]; }
Best Answer
0 Votes

Thank you! I will try it.

Best Answer
0 Votes

So I used the code you sent, then my clockface shows like this. Is there any thing I missed?F2-screenshot.png

Best Answer
0 Votes