简体   繁体   English

Nodejs 中的日期格式识别,Javascript

[英]Date Format Identification in Nodejs , Javascript

I have to find the format of date coming from various data sources.我必须找到来自各种数据源的日期格式。 What is the best way to find the format of date considering date , month and year can be in any order .考虑到日期、月份和年份可以按任何顺序查找日期格式的最佳方法是什么。

I need to determine the format by analysing some 10 random records from the table.我需要通过分析表中的一些随机记录来确定格式。


function isValidDate(str) {
    var d = moment(str,moment.ISO_8601);             
    if (d == null || !d.isValid()) 
    {
        return false;
    }

    match = ['YYYY-MM-DD','D/M/YYYY','DD/MM/YYYY','D/M/YY','DD/MM/YY','D-M-YYYY','DD-MM-YYYY','D-M-YY','DD-MM-YY','YYYY-MM-DD','YYYY-DD-MM',"MMMM D,YYYY","MMM Do,YYYY"] ;

    for(let i=0;i<12;i++)
    {
        if(str.indexOf(d.format(match[i])) >= 0 ) 
        {
            return match[i];
        }
    }
}

Try in this WAY.. This will help you...以这种方式尝试......这将帮助你......

providing date to moment will validate it, no need to pass the date Format.提供日期到时刻将验证它,无需传递日期格式。

var date = moment("2016-10-19");

And then进而

date.isValid() 

gives desired result.给出想要的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM