如何将数字数组转换为日期字符串格式。打字稿/角度7

时间:2019-11-10 18:09:12

标签: javascript angular typescript

是否可以将数字array解析为typescript中的日期字符串?

这就是我在C#中的做法:

    int[] date = { 2017, 10, 20 };

    public string convert(dynamic date)
    {
        return string.Format("{0}/{1}/{2}", date[1], date[2], date[0]);
    }

typescript / javascript中有任何建议吗?

2 个答案:

答案 0 :(得分:2)

与您的C#代码最相似的可能是:

const date = [2017, 10, 20];

convert(date: number[]): string {
  return `${date[0]}/${date[1]}/${date[2]}`;
}

答案 1 :(得分:1)

您可以尝试以下操作:

let date = [2017, 10, 20]
const convert = function(d){
  return d.join('/')
}

或者,对于更通用的解决方案,您应该检查Date内置对象。