user663724
user663724

Reputation:

How to make the variable global in this case?

I am displaying yesterdays date with the help of this below code , all this works fine , but my requirement is that i need to display the variable yesterday as global , because i want to use that variable for other purposes also

<!DOCTYPE html>
<html>
<head>
<script>
function displayDate()
{
var d = new Date();
d.setDate(d.getDate() - 1);
var curr_date = d.getDate(); 
var curr_month = d.getMonth() + 1; 
var curr_year = d.getFullYear();
var yesterday = curr_year + "-" + curr_month + "-" + curr_date;
alert('The value after modifications is'+yesterday )
}
</script>
</head>
<body>
<p id="demo">New Modifications FOR Click Button to Display Yesterday Date</p>
<button type="button" onclick="displayDate()">Display Date</button>
</body>
</html> 

Please tell me how can i display the variable yesterdayday as global in this example , thanks in advance .

Upvotes: 1

Views: 84

Answers (4)

webrama.pl
webrama.pl

Reputation: 1890

You can even complicate it but in a nice way I think:

<!DOCTYPE html>
<html>
<head>
<script>

function getYesterDay(values)
{
    if(values)
        getYesterDay.date = values;
    else
        return getYesterDay.date;
}
function displayDate()
{
    var d = new Date();
    d.setDate(d.getDate() - 1);
    var curr_date = d.getDate(); 
    var curr_month = d.getMonth() + 1; 
    var curr_year = d.getFullYear();
    getYesterDay(curr_year + "-" + curr_month + "-" + curr_date);
    alert('The value after modifications is'+getYesterDay() )
}
</script>
</head>
<body>
<p id="demo">New Modifications FOR Click Button to Display Yesterday Date</p>
<button type="button" onclick="displayDate()">Display Date</button>
</body>
</html> 

Use getYesterDay() function to set or get date everywhere you want.

Upvotes: 0

Guffa
Guffa

Reputation: 700342

There are basically two ways.

You can keep from declaring it as a variable inside the function. Then it will be created as a global variable when you use it:

function displayDate()
{
...
  yesterday = curr_year + "-" + curr_month + "-" + curr_date;
...
}

A way that shows the intention more clearly, is to declare it as a variable outside the function:

var yesterday;

function displayDate()
{
...
  yesterday = curr_year + "-" + curr_month + "-" + curr_date;
...
}

For completeness, I want to mention a third possible way. You can also create it as a property of the window object. As that is the global scope, the property works as a global variable in almost all aspects:

function displayDate()
{
...
  window.yesterday = curr_year + "-" + curr_month + "-" + curr_date;
...
}

Upvotes: 0

Dhaval Marthak
Dhaval Marthak

Reputation: 17366

<script>
var yesterday;
function displayDate()
{
...........
}
</script>

FYI: by having prefix var makes variable global.

Refer to some javascript tutorials

Upvotes: 0

eric.itzhak
eric.itzhak

Reputation: 16062

Simple. like this :

var yesterday;
function displayDate()
 {
    var d = new Date();
    d.setDate(d.getDate() - 1);
    var curr_date = d.getDate(); 
    var curr_month = d.getMonth() + 1; 
    var curr_year = d.getFullYear();
    yesterday = curr_year + "-" + curr_month + "-" + curr_date;
    alert('The value after modifications is'+yesterday )
 }

You should read some tutorials about JS in general though.

Upvotes: 5

Related Questions