Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to use String .split() and .match()

Hi all,  my JS skills aren't the best, so whilst I know the code to use for .split() and .match() commands, every time I use these I get a runtime error:

 

[2:26:27 PM]Unhandled TypeError: Expected a function.

Is there a specific Import reference I need to add to get these functions to be supported?

 

Right now, I have

export let getDayFilename = function() {

  console.log(week.selectedDay);
  var date = week.selectedDay.split(" ");
  
  //Get the year
  var filename = date[3];

where week.selectedDay is "Mon Oct 15 2018 01:00:00 GMT+01:00". The console.log works fine, but then I get the error above.

 

Can someone help a confused noob out? Thanks 😉

 

FYI: I am ultimately converting the long date string from "Mon Oct 15 2018 01:00:00 GMT+01:00" to (e.g.) "2018-10-15" and using this as a file name.

Best Answer
0 Votes
1 REPLY 1

I think your code must be failing elsewhere. This works for me:

 

var week = {}
week.selectedDay = "Mon Oct 15 2018 01:00:00 GMT+01:00";
var date = week.selectedDay.split(" ");

//Get the year
var filename = date[3];
console.log(filename);  // 2018

///////// or

var newDate = new Date(week.selectedDay);
console.log(newDate.getFullYear()); // 2018
Best Answer
0 Votes