10 examples of 'how to get current year in javascript' in JavaScript

Every line of 'how to get current year in javascript' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
449function getYear(){
450 var year = "";
451 if(document.title)
452 {
453 var mName = document.title.substr(0, document.title.indexOf(" -")).replace(/ /g, "+");
454 year = mName.substr(document.title.indexOf("(")+1).replace(")","");
455 }
456 return year;
457}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

42function getYear(date) {
43 return moment(date).jYear();
44}
96public getCurrentDate(): string {
97 /* Return current year(string) to display in calendar header. */
98 return this.state.date.year().toString();
99}
7showCurrentYear() {
8 return new Date().getFullYear();
9}
5function completeYear(year) {
6 if(year.length == 2) {
7 var currentYear = new Date().getFullYear();
8 if (parseInt(year) <= currentYear % 100)
9 return Math.floor(currentYear/100) + year;
10 else
11 return (Math.floor(currentYear/100) - 1) + year;
12 }
13 return year
14}
129public getCurrentYear() {
130 return new Date().getFullYear();
131}
8getCurrentYear() {
9 return element(by.css('.signature .year')).getText();
10}
66private getStartingYear(year:number) {
67 // todo: parseInt
68 return ((year - 1) / this.datePicker.yearRange) * this.datePicker.yearRange + 1;
69}
93export function dayOfYear(date: Date): number {
94 const dayMs = 1000 * 60 * 60 * 24;
95 const yearStart = new Date(date.getFullYear(), 0, 0);
96 const diff =
97 date.getTime() -
98 yearStart.getTime() +
99 (yearStart.getTimezoneOffset() - date.getTimezoneOffset()) * 60 * 1000;
100 return Math.floor(diff / dayMs);
101}
80function getDayOfYear(date,utc) {
81 utc = utc || false;
82 var dayCount = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
83 var mn = utc ? date.getUTCMonth() : date.getMonth();
84 var dn = utc ? date.getUTCDate() : date.getDate();
85 var dayOfYear = dayCount[mn] + dn;
86 if(mn > 1 && isLeapYear(utc ? date.getUTCFullYear() : date.getFullYear())) dayOfYear++;
87 return dayOfYear;
88}

Related snippets