JavaScript。使用子字符串和indexof将MM-DD-YYYY的用户输入转换为DD-MM-YYYY

JavaScript。使用子字符串和indexof将MM-DD-YYYY的用户输入转换为DD-MM-YYYY,javascript,eclipse,indexof,substring,Javascript,Eclipse,Indexof,Substring,好的,所以我需要写一个程序,用户输入MM-DD-YYYY,程序输出重新排列的日期,如DD-MM-YYYY。我被告知要使用substring和indexOf,但我不知道如何将这两者合并在一起以获得我想要的输出。 ex) 输入:2016年4月11日 输出:2016年4月11日您可以使用indexOf/lastIndexOf查找字符串中空格的索引。一旦有了索引,就可以使用子字符串拉出并分离字符串 例如: var date = "April 11, 2016"; var index = date.ind

好的,所以我需要写一个程序,用户输入MM-DD-YYYY,程序输出重新排列的日期,如DD-MM-YYYY。我被告知要使用substring和indexOf,但我不知道如何将这两者合并在一起以获得我想要的输出。 ex)

输入:2016年4月11日
输出:2016年4月11日

您可以使用indexOf/lastIndexOf查找字符串中空格的索引。一旦有了索引,就可以使用子字符串拉出并分离字符串

例如:

var date = "April 11, 2016";
var index = date.indexOf(" ");
var month = date.substring(0, index);
console.log(month); // 'April'

这应该让你开始。一旦你把字符串分开,你就可以按照你想要的顺序把它们连在一起。

因为我很懒,可能类似于:

var input = "April 11, 2016";
var arr = input.split(" ");

// get rid of that pesky comma
arr[1] = arr[1].slice(0, -1);

// this is embarrassing but I said I'm lazy!
var output = arr[1] + " " + arr[0] + " " + arr[2];
console.log(output);

您指定的输入格式为MM-DD-YYYY,但示例输入格式为2016年4月11日。这是哪一个?你可以从了解什么和做什么,以及如何使用它们开始。