6

I have string in the following format "30.11.2019". I need to transform it into a Date and get the short year representation (last 2 digits from year) like "19". The following code doesn't work

var strDate = new Date("30.11.2019");
var shortYear = strDate.getFullYear(); 
Vytsalo
  • 670
  • 3
  • 9
  • 19

6 Answers6

8

new Date() does not work with a single string argument in that format. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Easiest way is to call it with 3 arguments (year, month, day). Do note that month is the month index (0 based), so November (11th month) is actually 10th in the format that Date expects.

  new Date(2019, 10, 30).getFullYear() % 100;
  // returns 19;

If you can't do it this way and you simply must work around the string format mentioned, then you can just do

const dateString = '30.11.2019';
const year = dateString.substring(dateString.length-2);
bamtheboozle
  • 5,847
  • 2
  • 17
  • 31
8

I'm not entirely sure if you want only short representation of the year or whole date, BUT with short representation of the year on it - if so, then I would suggest using toLocaleDateString method:

new Date(2019, 10, 30).toLocaleDateString('pl', {day: 'numeric', month: 'numeric', year: '2-digit'})

It will return you:

"30.11.19"

or if you want to get the short year date only:

new Date(2019, 10, 30).toLocaleDateString('en', {year: '2-digit'})

it will return you:

"19"
lukaszkups
  • 5,790
  • 9
  • 47
  • 85
3

You can get last two digits with the following code:

var strDate = new Date(); // By default Date empty constructor give you Date.now
var shortYear = strDate.getFullYear(); 
// Add this line
var twoDigitYear = shortYear.toString().substr(-2);
Stefan Popovski
  • 486
  • 6
  • 22
1

Since the string you're using isn't in a format recognized by Date.parse() (more on that here), you need to manually create that Date object.

For example:

const strDate = '30.11.2019';
let [d,m,y] = strDate.split(/\D/);
const date = new Date(y, --m, d);

console.log(date.getFullYear())

You can then use Date.getFullYear() to get the year and extract the last two digits, as you need.

  • @RobG that is so much more elegant! Updating the answer accordingly :) – Giant Robot Nov 29 '19 at 09:22
  • Why do you need --m in `new Date(y, --m, d)`? – ndrewl Nov 29 '19 at 09:56
  • 1
    @ndrewl the second argument to Date() is an integer value representing the month, beginning with 0 for January to 11 for December. Since the numeric representation of the month in the original string begins with 1 for January, we need to reduce it by `1` before we pass it to Date(). Does that answer your question? – Giant Robot Nov 29 '19 at 10:01
  • Yes, thanks! I'm currently learning JavaScript, and it seemed a bit unnatural to me that days start at 1, but months at 0. If someone else is interested, here is the reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date – ndrewl Nov 29 '19 at 10:07
0
var strDate = "30.11.2019";
var lastdigit = strDate.slice(-2);
Cpt Kitkat
  • 1,392
  • 4
  • 31
  • 50
  • While this code may answer the question, providing additional context regarding *why* and/or *how* this code answers the question improves its long-term value. – Tân Nov 29 '19 at 17:11
0

do not need split string, I think. using moment

yarn add moment

const moment = require( 'moment' );

const simpleYear = moment( "30.11.2019", "DD.MM.YYYY" ).format( "YY" );
console.log( "simpleYear = " + simpleYear );