11 Replies - 942 Views - Last Post: 09 July 2012 - 10:50 PM

question: How do I get the number of day and month

Posted 27 June 2012 - 02:37 PM

I am trying to create a script that will take the day and month and change my website's asp.net theme to match the time of year or holiday when someone loads the page. I know how to grab the entire date time but how do i just grab the day/month? here is my code so far its small but this is just after an hour of fiddling around. I have searched the web but can't seem to f ind info on how to exactly do this.

Replies To: question: How do I get the number of day and month

Re: question: How do I get the number of day and month

I'm confused. Is this supposed to be Javascript? Looks kind of like you copied a C# snippet and just replaced the method visibility keyword with function.

Either way, here are a few things that apply to both:

They are case-sensitive. There is a difference between else and Else. (And the latter is incorrect.)

On line #4 you use a c variable, but it doesn't seem to exist in that scope. (Is it a global Javascript variable, or a C# class attribute?)

On line #12 you use str.Month where I assume you mean strMonth.

In all you else if conditionals you compare strMonth to a number, but on line #2 you clearly assign it a string value.

In all of your conditional clauses - with the exception of the last else clause - you assign a word to the document.Page.Theme. However they are not quoted, which indicates that they are in fact variables. - What are hey supposed to be? Strings? Variables? If they are variables, where are they defined?

In the aforementioned else clause you assign default to page.theme. First of all, the default keyword is reserved. You can not use it as a variable name, and thus can not assign it to variables. (It's used in switch statements.) - Also, why do you set page.theme this time, instead of document.Page.Theme like in the others?

This post has been edited by Atli: 27 June 2012 - 03:12 PM
Reason for edit:: Finished the first sentence.

Re: question: How do I get the number of day and month

Posted 27 June 2012 - 03:36 PM

yah i its suppose to be java script and i kept getting an error saying i had no return type listed so i added the public string and its stopped. i caught the the types like the c and the period. I removed the last else. here is the newest version of what i am trying.

Re: question: How do I get the number of day and month

yah i its suppose to be java script and i kept getting an error saying i had no return type listed so i added the public string and its stopped.

OK, I think you are confusing Java and Javascript. Those are two very different things. In Java you need to define return types for methods, but not in Javascript. I don't know what IDE you are using to code this, but if it complains about missing return types and then quiets down when you make that adjustment, you are more than likely using a Java IDE. That won't help you at all when writing Javascript code.

Re: question: How do I get the number of day and month

Posted 27 June 2012 - 05:14 PM

*grumbles softly to himself*

The file is a javascript.JS file its not java and its not C#. Apparently instead of helping me with my "Java Script" issue they decided to change what I was talking about, assume I must have been dumb, then re-categorize and move my forum post.

I am creating a "Javascript" file for an ASP.Net webpage that will , on loading, change the sites theme to one matching the season or holiday.

Re: question: How do I get the number of day and month

Apparently instead of helping me with my "Java Script" issue they decided to change what I was talking about, assume I must have been dumb, then re-categorize and move my forum post.

A lot of people confuse Java and Javascript based on the names. It's an easy mistake to make. Judging by your code examples - which look very much like C# and/or Java - it was the most rational conclusion. - Even if I had been right, I wouldn't have assumed you were dumb, only that you were new to these concepts.

As to why the thread was moved here. BetaWar must have misread your post and assumed you accidentally posted a Java question in the Javascript section. It happens all the time. I'll move it back, now that we are positive that this is indeed Javascript we're dealing with.

plasticmonkey007, on 28 June 2012 - 12:14 AM, said:

I reiterate it isn't a java file its a Javascript file.

P.S. to be precise the file's name is JScript1.js.

OK, so your problem is that you are using incorrect Javascript syntax. I don't know how well VS2010 supports Javascript, but judging by your previous posts it seems it's either overly sensitive to non-errors (like missing return statements from functions, which is in fact entirely optional) or simply trying to parse it like C#. Either way, I suggest you don't rely on it to much.

Javascript functions are defined like this:

function functionName() {
// Do stuff here.
// You can return things, or not. There is no need to
// change the syntax of the function either way.
}

There are no visibility or type keywords used, only the function keyword.

Also, this line in your code seems out of place:

intMonth = System.DateTime.Today.Month;

The System.DateTime type is a part of the .NET framework and is not available to Javascript code. The Javascript Date function provides similar functionality for Javascript code.

during school they were a bit chaotic in the order of languages they taught us and only touched on Javascript briefly. I did get my syntax confused because i must have took too much of a break from coding and it got mixed up in my head. The thing is that in my second post to this topic in the first sentence i state that "I am using java script"

ignore the line that reads "var config = '<%=ConfigurationManager.AppSettings["Theme"] %>'
I was attempting to experiment with some information I got on another site.

Re: question: How do I get the number of day and month

OK, this would be valid Javascript code. The main problems there are the var Month declaration, as I mentioned before, and the seemingly undefined variables you are using.

Do you define the variables outside the function you posted? The intMonth number and the page object aren't set in the function.

I'm also not very familiar with ASP.NET, so the I'm not sure exactly how you intend to actually change the theme. How does that work? - The Javascript code is executed long after the server-side ASP.NET page has completed it's execution, so affecting changes to that won't work. You'd have to make changes to the client-side code, like setting CSS values or just swapping entire CSS files.

plasticmonkey007, on 28 June 2012 - 12:48 AM, said:

The thing is that in my second post to this topic in the first sentence i state that "I am using java script"

That's true, but that isn't always reliable. People new to Java sometimes don't realize that Javascript is a separate language. They say "java script" when they in fact mean "java code". Separating the words -- "java script" rather than "Javascript" -- tends to be an indication of this.

Re: question: How do I get the number of day and month

Hi,
actually all you have to do two things
first is to print the System.Date.Today.Month to the response the same way you did for the ConfigurationManager.AppSettings["Theme"]

second is to assign returned value to the intMonth not to the Month as Atli mentioned before
so, when the server reads this page it will parse and execute the asp tags and output the month value, after that the client will receive the page with the new changes and the intMonth will be assigned the value generated by the server as if it was hard coded

var intMonth = <%= System.Date.Today.Month %>;

note:
the(=) sign after the asp opening tag is a shorthand for response.write()