/    Sign up×
Community /Pin to ProfileBookmark

Extract Year From Date

Hi,

How can i extract Year value from a date field in Javascript. For eg., i need to get ‘2005’ from ’01/04/2005′.

Can anyone help me in this ?

Thanks and Regards,

Nirmal

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@KorJul 08.2005 — var date = '01/04/2005';

var year = date.split('/')[date.split('/').length-1];

Note that the variable year is a string. If you need a number use Number() method.
Copy linkTweet thisAlerts:
@nirmalnarayanauthorJul 08.2005 — Hi,

Thanks for your help. As i am not from Europe i don't understand the Euro politics.

Thanks & Regards,

Nirmal
Copy linkTweet thisAlerts:
@CharlesJul 08.2005 — <script type="text/javascript">

var date = new Date ('01/04/2005');
alert (date.getFullYear());

</script>
Copy linkTweet thisAlerts:
@KorJul 08.2005 — 
...date field...
[/quote]

nirmalnarayan has not specified if the field value is dd/mm/yyyy or mm/dd/yyyy, so that I have considered safely to use split() method
×

Success!

Help @nirmalnarayan 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.25,
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,
)...