0

I currently have the following script located in my .cshtml file:

<script type="text/javascript">
    var d = new Date();
    var month = d.getMonth;

    window.onload = function () {
        document.getElementById("currentMonth").innerHTML = month;
    }
</script>

Now, I would like to access currentMonth inside of my span here:

<span style="font-size: 250%; color:red;">CURRENTMONTH HERE</span>

How do I do I access my javascript variable in plain text inside <span> or am I going about this the wrong way?

What I am trying to accomplish is to display the current month in string format inside of the span. I have tried using @HttpContext.Current.Timestamp.Month as well and this prints a 2 to the screen but the ToString() for it does not work. I have never used Javascript before so please go easy.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Chisx
  • 1,976
  • 4
  • 25
  • 53
  • There is no `currentMonth` variable in your JavaScript, but even if it is there you can't do that in server side code. Clarifying what you actually want to achieve may open up some alternative solutions. – Alexei Levenkov Feb 24 '16 at 02:52
  • I am just attempting to print the current month inside the span. I am using an application that has DayPilot calendar added but cannot resolve how to retrieve the currentMonth from it. So I resolved to using the current system time. – Chisx Feb 24 '16 at 02:56
  • Have you tried using `window.month = d.getMonth();` and then using `window.month` in span – Abhinav Galodha Feb 24 '16 at 02:56
  • no I have not, you can just set a window property month to the current month? – Chisx Feb 24 '16 at 02:57
  • You're calling the getMonth without () which is wrong. You have no currentMonth id anywhere, not sure what you're trying to accomplish. Can you post more info? – dmeglio Feb 24 '16 at 02:57
  • yes, you can set any property to window object. However, you may try to nest Property inside a namespace for conflicts. – Abhinav Galodha Feb 24 '16 at 02:59
  • I tried your method as well but inside of span tag it did not register window.month. Is there special way to reference window attributes? – Chisx Feb 24 '16 at 03:03

1 Answers1

3

I don't think you need javascript for this. You can render the current date from the server. Try this:

<span style="font-size: 250%; color:red;">@DateTime.Today.ToString("MMMM")</span>

And drop the Javascript.

EDIT: for the sake of completeness... If you want to access the name of the current month from Javascript, you can do so like this.

var monthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
];

var d = new Date();
document.write("The current month is " + monthNames[d.getMonth()]);

Code shamelessly ripped from Get month name from Date

Community
  • 1
  • 1
Kevin Burdett
  • 2,892
  • 1
  • 12
  • 19
  • thank you this worked. Is this bad practice? or is this okay to retrieve time in this manner. – Chisx Feb 24 '16 at 03:01
  • It is precisely what DateTime.Today is for :) The only thing to be careful of is crossing timezones, since this time is rendered in the server's timezone. However, since you are only using the month, it is a very small edge case. – Kevin Burdett Feb 24 '16 at 03:03
  • NP, I added a javascript example, just in case you have need of this from javascript later... – Kevin Burdett Feb 24 '16 at 03:09
  • 1
    @KevinBurdett It is much better to point to SO resource which generally cover all cases - http://stackoverflow.com/questions/1643320/get-month-name-from-date is way more comprehensive than W3Schools one (and way less controversial http://meta.stackoverflow.com/questions/280478/why-not-w3schools-com) – Alexei Levenkov Feb 24 '16 at 03:24
  • Thanks for the note @AlexeiLevenkov. I have updated the answer with your suggestion and will remember to prefer SO in the future. – Kevin Burdett Feb 24 '16 at 03:28
  • 1
    @KevinBurdett—that "edge case" is a **certainty** that will last for up to about 12 hours every month and will be early or late, depending on the timezone difference between the client and server. – RobG Feb 24 '16 at 04:42
  • You are correct @RobG. Most apps don't see worldwide usage, so the window is unlikely to be that large. However, I should not have assumed that. I don't know what the OP's user base looks like, or the criticality of the data. This issue could range from occasional display oddity to production downtime. Thanks for the "severity bump". – Kevin Burdett Feb 24 '16 at 04:52