在 DOJO 中格式化 ddmmyy

Formatting ddmmyy in DOJO

我有一个 JSON 文件,其中包含以下键值对。

"emvtag1"="currentdate"
"emvtag2"="currenttime"

我需要使用 DOJO 来替换当前日期和当前时间。

这是我的代码:

formatDate: function(d) {
  //get the month
  var month = d.getMonth();
  //get the day
  //convert day to string
  var day = d.getDate().toString();
  //get the year
  var year = d.getFullYear();

  //pull the last two digits of the year
  year = year.toString().substr(-2);

  //increment month by 1 since it is 0 indexed
  //converts month to a string
  month = (month + 1).toString();

  //if month is 1-9 pad right with a 0 for two digits
  if (month.length === 1) {
    month = "0" + month;
  }

  //if day is between 1-9 pad right with a 0 for two digits
  if (day.length === 1) {
    day = "0" + day;
  }

  //return the string "MMddyy"
  return month + day + year;
}

SResp: function(act) {
  var event = JSON.parse(data);
  if (action === "okEMVHost") {
    var d = new Date();
    emvtag1 = this.formatDate(d);
    emvtag2 = this.formatDate(d);

  }
}

我知道它在 JAvascript 中是如何工作的,formatteddate(d) 函数,不确定它在 DOJO 中是否会像这样。

在 Dojo 中,为了格式化日期,使用 dojo/date/locale ::format() 函数,如下所示,

locale.format( date, {selector:"date", datePattern:"ddMMyy" } );

您可以看到上面的片段使用不同的格式

require(["dojo/date/locale"
], function(locale) {
  var date = new Date();
  
  var format1 = locale.format( date, {selector:"date", datePattern:"ddMMyy" } );
  
  var format2 = locale.format( date, {selector:"date", datePattern:"MM-dd-yyyy" } );
  
    var format3 = locale.format( date, {selector:"date", datePattern:"MM / dd / yyyy ss:mm:SSS" } );
    
    
  console.log("ddMMyy -----> ", format1);
  console.log("MM-dd-yyyy -> ",format2);
  console.log("MM /dd/yyyy ss:mm:SSS -> ",format3);
  
});
<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>