Author Archives: Larry Battle

Code of the Day: Javascript Date.prototype.setDay()

In Javascript, I find it strange how `Date.prototype.getDay()` in defined but `Date.prototype.setDay()`.
So here’s the code for `Date.prototype.setDay()`.

// Sets the Date object to the day of the week.
// @param {Number} - dayIndex must be from 0 - 6, where 0 is sunday.
// @return {Number} returns the primitive value of the Date object.
Date.prototype.setDay = Date.prototype.setDay || function (dayIndex) {
	dayIndex = Math.floor(+dayIndex);
	if (dayIndex < 0 || 6 < dayIndex) {
		throw new Error("Must pass integer between 0-6, where sunday is 0.");
	}
	this.setDate( this.getDate() + dayIndex - this.getDay() );
	return this.valueOf();
};

Demo 1

var a = new Date(Date.parse("Feb 1, 2014"));
a.setDay(0);
console.log(a.toDateString() === "Sun Jan 26 2014" );
a.setDay(6);
console.log(a.toDateString() === "Sat Feb 01 2014" );

Demo 2

// Prings the Monday - Friday for the current day.
var getWeekString = function () {
	var days = [],
		date = new Date();
 
	for (var i = 1, len = 6; i < len; i++) {
		date.setDay(i)
		days.push("## " + date.toDateString() );
	}
	return days;
};
console.log(getWeekString().join("\n\n"));

If you looking for a good Javascript Date library, checkout moment.js.

More info:
MDN Date Object

Larry Battle

Hello, I'm Larry Battle and I love to program, fix problems and discover new technologies. Check out my stackoverflow and github accounts. I also do book reviews on amazon.com. I'm not the best of writers but I do enjoy spreading my knowledge through my short blogs at bateru.com/news. So please leave some feedback. It would be greatly appreciated.

More Posts - Website

Follow Me:
Twitter

Links Teach Me Startup

Hey you all,
I’m offering an service call “Links Teach Me”.
Basically it’s where you give a topic for school or an certication and I’ll provide you a list of resources to make speed up your learning.
If you’re looking for the best review material, audio or video tutorials, online flash cards, online communities for a topic then contact me.
You can purchase the service on fiverr.com for $5.
There are only 8 slots available to reserve your spot today!

Request Service

I will provide best resources for learning a topic for $5

Larry Battle

Hello, I'm Larry Battle and I love to program, fix problems and discover new technologies. Check out my stackoverflow and github accounts. I also do book reviews on amazon.com. I'm not the best of writers but I do enjoy spreading my knowledge through my short blogs at bateru.com/news. So please leave some feedback. It would be greatly appreciated.

More Posts - Website

Follow Me:
Twitter

Social Network in 30 days – End

I’m ending the challenge today due to lack of progress.
From the last update, I’ve switched from developing the site in Google go to Meteor, for node.js, and then back to Google Go.
The problem will development was that I didn’t devote enough time each day to complete each feature. This made catching up overwhelming and caused me to stress out about the project. It also didn’t help that I was a one man team with a full time job.

:(
:(

Oh well. I’ll be re-starting this challenge next month. Let’s see what happens.

Larry Battle

Hello, I'm Larry Battle and I love to program, fix problems and discover new technologies. Check out my stackoverflow and github accounts. I also do book reviews on amazon.com. I'm not the best of writers but I do enjoy spreading my knowledge through my short blogs at bateru.com/news. So please leave some feedback. It would be greatly appreciated.

More Posts - Website

Follow Me:
Twitter

Social Network in 30 days : Day 8 – Day 10

I haven’t had time to work on the site, LinksTeach.me, for sometime now. So what I’m going to do is delay development for 3 days to finish up a few other side projects and complete essential training.
I’ll startup backup on monday and plan to release an alpha version on Nov 11, 2013.

Awesome bash script:

find . -name main.go | xargs -L1 sed -ri "1s/^\/\/.*[^\.]$/&\./"`


The command finds all the files with the name main.go within the current directory.
After which, for each file it adds a dot to the end of the first line if it’s a comment and doesn’t need with a period.
Why is this useful?
`godoc` is a tool for creating documentation for Google Go files.
Example:
Source code for errors
Documentation for errors

`godoc` will use the first sentence as the description for a package. Since I didn’t end the first line with a period, then the entire comment on multiple lines becomes the description for the package.

So when run the awesome bash command above, it corrects the package comment each main.go like so.
Example.

// Example code for Chapter 2.3 from "Build Web Application with Golang"
// Purpose: Creating a basic function


to this.

// Example code for Chapter 2.3 from "Build Web Application with Golang".
// Purpose: Creating a basic function

Larry Battle

Hello, I'm Larry Battle and I love to program, fix problems and discover new technologies. Check out my stackoverflow and github accounts. I also do book reviews on amazon.com. I'm not the best of writers but I do enjoy spreading my knowledge through my short blogs at bateru.com/news. So please leave some feedback. It would be greatly appreciated.

More Posts - Website

Follow Me:
Twitter