Skip to content

All topics  /  JavaScript

Moment JS Get Year from Date Example

JavaScript ·

Hi Dev,

Now let's see example of moment js get year from date. we will show moment js get year from date. we will discuss about get year from date moment jquery. i explained simply about jquery moment get year from date. Here, Creating a basic example of how to get year from date in moment js.

Here, i will give you simple example of jquery moment js get year from date using format('YYYY').

So let's see both example with output:


Example

<!DOCTYPE html>
<html>
<head>
    <title>Jquery Moment Example - NiceSnippets.com</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" crossorigin="anonymous"></script>
</head>
<body>
    <h1>Jquery Moment Example - NiceSnippets.com</h1>
</body>
<script type="text/javascript">
    var oneDate = moment('02-01-2021', 'DD-MM-YYYY');
    var yearName = oneDate.format('YYYY');
    console.log(yearName);
    var oneDate = moment();
    var yearName = oneDate.format('YY');
    console.log(yearName);
</script>
</html>

Output:

2021
21

It will help you....