/    Sign up×
Community /Pin to ProfileBookmark

current_month -1 display in javascript

Hi friends,

I am new to javascript.I am looking for a code which will display current_month – with year in the format “mon/yyyy” when I execute today.

example “Dec,2008” if executed in the Jan,2008

Please help me in providing the code.Thanks in Adv..!!!!!

Kbasha2004

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@rgrneJan 27.2008 — You can assign the Javascript Date object to a variable, for example

var currentdate = new Date();

Then you have to get the current month and year out of it like this:

thismonth = currentdate.getmonth();

(getmonth gives you the month as a number from 0 to 11).

thisyear = currentdate.getFullYear();

(getFullYear gives you the year as a four digit number).

To get the full name of the month I think you have to write a little lookup table in an array or something to make a variable writtenmonth = "January" if thismonth=0, etc. I don't think there is an built in function that provides the month as a name.

In the end you just print out the variables and put a slash between them.
Copy linkTweet thisAlerts:
@JMRKERJan 27.2008 — May also need to add a little bit more logic if you are trying to display the month before January as it will be December of the PREVIOUS year.

Fairly easy test to check as Jan is month 0 and (Jan-1) will be -1 for test

with a change to getFullYear() value.
Copy linkTweet thisAlerts:
@mrhooJan 27.2008 — [CODE]Date.monthnames= ['January','February','March','April','May','June','July',
'August','September','October','November','December'];

Date.prototype.getLastMonth= function(){
var m= new Date(this.setMonth(this.getMonth()-1));
return Date.monthnames[m.getMonth()].substring(0,3)+'/'+m.getFullYear();
}[/CODE]


//test code?ate.monthnames is language specific

[B]alert(new Date().getLastMonth());[/B]
×

Success!

Help @kbasha2004 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...