Search code examples
javascriptdatetimedojo

Dojo cannot parse custom date time format


I am attempting to use Dojo locale date parser but am having a heck of a time. Every time I do it it returns null.

I even forced a value of "test" to the variable outside of the function to eliminate an undefined variable scope, and it still returns null.

var djLastString = "test";
require(["dojo/date/locale"], function(locale) {
    djLastString = locale.parse("20180511 18", {
        datePattern: 'yyyyMMdd',
        timePattern: 'HH',
        selector: 'date'
    });
});
console.log(djLastString);

result: null


Solution

  • You want to convert string into Date so , in your case you have to specify the exact pattern that goes with your string ,

    Your string is 20180511 18 so the datePattern should be yyyyMMdd HH ( H -> hours ) instead of yyyyMMdd

    See below snippet

    var djLastString = "test";
    require(["dojo/date/locale"], function(locale) {
        djLastString = locale.parse("20180511 18", {
            datePattern: 'yyyyMMdd HH',
            timePattern: 'HH',
            selector: 'date'
        });
        
        console.log(djLastString);
        console.log(djLastString.getTime());
    });
    html,
    body {
      width: 100%;
      height: 100%;
      margin: 0;
    }
    
    #accContainer {
      height: 100% !important;
    }
    <link href="//ajax.googleapis.com/ajax/libs/dojo/1.10.0/dijit/themes/claro/claro.css" rel="stylesheet" />
    <script>
      dojoConfig = {
        parseOnLoad: true,
        async: true
      };
    </script>
    <script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.0/dojo/dojo.js"></script>