8

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(); 
1
  • simple modulo 100 operation Commented Nov 29, 2019 at 8:53

6 Answers 6

13

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"
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for using .toLocaleDateString() this is what it was built for.
10

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);

1 Comment

% 100 only works with years from XY10 to XY99 (but not for years from XY00 to XY09) without padding to get the desired 2 digits.
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);

Comments

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.

4 Comments

@RobG that is so much more elegant! Updating the answer accordingly :)
Why do you need --m in new Date(y, --m, d)?
@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?
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: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
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 );

Comments

0
var strDate = "30.11.2019";
var lastdigit = strDate.slice(-2);

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.