解析工作日名称始终显示"星期一"使用d3.time.format

时间:2017-05-19 16:22:15

标签: javascript d3.js

我使用的是d3 v3.x,我想使用d3.time.format解析工作日名称,然后使用locale.timeFormat方法进行翻译。

这是加泰罗尼亚语翻译的一个例子:

var LOCALES = {
en: d3.locale({days:["Sunday", "Monday", "Tuesday", 
"Wednesday", "Thursday", "Friday", "Saturday"]}),
ca: d3.locale({days:['Diumenge', 'Dilluns', 'Dimarts', 
'Dimecres','Dijous','Divendres', 'Dissabte'"]})
};

var locale = LOCALES["ca"];
var weekdayParser = locale.timeFormat("%A");

var daysOfTheWeek = ["Monday", "Tuesday", "Wednesday", 
"Thursday", "Friday", "Saturday", "Sunday"];

for(var i=0; i<daysOfTheWeek.length; i++){ 
daysOfTheWeek[i] = 
weekdayParser(d3.time.format("%A").parse(daysOfTheWeek[i]));
};

结果:

daysOftheWeek = ["Dilluns", "Dilluns", "Dilluns", 
"Dilluns", "Dilluns", "Dilluns", "Dilluns"];

使用这种方法有什么错误吗?

2 个答案:

答案 0 :(得分:1)

虽然您可以将一周中的某一天传递给d3.time.format()。解析,但它不会执行任何操作。 D3使用除了一周中的所有内容(%A%a%w)来解析日期。

这会导致解析时间尴尬:

var format = d3.time.format("%A-%y-%m-%d");
console.log(format(format.parse("Monday-17-05-17"))); // "Wednesday-17-05-17"

并且,正如您所说:

format = d3.time.format("%A");
console.log(format(format.parse("Wednesday"))); // "Monday"

以及:

format = d3.time.format("%a");
console.log(format(format.parse("Wed"))); // "Mon"

format = d3.time.format("%w");
console.log(format(format.parse("5"))); // "1"

为什么星期一?

使用d3解析时的默认时间是1900-01-01 0:0:0.000。解析日期时,会修改这些数字;如果仅修改年份和月份,则日期仍为1.由于解析星期几不会修改任何这些数字,因此日期仍为1900-01-01,正如命运所示,它是Dilluns。

答案 1 :(得分:0)

非常感谢您的回答,但它没有为我提供解决方案。但是,你给了我解决方案的想法。考虑到D3库的限制,我希望它对其他人有用。

这是我的解决方案:

   import flash.net.SharedObject;

   stage.addEventListener(KeyboardEvent.KEY_DOWN, buttonPressed); 
   var stateSO:SharedObject = SharedObject.getLocal("saveState");
   if (stateSO.size > 0 && stateSO.data.lastFrame && stateSO.data.lastFrame != undefined) {
  //the shared object exists and has a valid value, so lets skip to that frame
 gotoAndPlay(stateSO.data.lastFrame);
 trace("Load Here.");
 }


 function buttonPressed(event:KeyboardEvent):void{  
 if (event.keyCode == Keyboard.MENU){

 //this function runs every frame 
 function saveState(Event){       
 stateSO.flush(); //save the cookie (shared object)
 trace("Save Here.");
 return;
 }

 addEventListener(Event.ENTER_FRAME, saveState);

 stage.removeEventListener(KeyboardEvent.KEY_DOWN, buttonPressed);
 } 

 }

结果:

/*
  ##Adding the days of the week to the calendar##
  Only a year and month is needed,
  the different days of the week are added inside the for loop,
  starting by the first day "1" of the month
*/
  var parseDate = d3.time.format("%Y%m%d").parse;
  var weekdayParser1 = d3.time.format("%w"); //parser for day numbers
  var weekdayParser2 = locale.timeFormat("%A"); //parser for day names

  var yourTime = "201704";
  var daysOfTheWeeK = [];

  for(var i=0; i<7; i++){
      //Adding the days of the week according to the parsed Date "yourTime"
      daysOfTheWeeK.push({
          "weekdayNumber": weekdayParser1(parseDate(Number(yourTime+"01")+i+"")),
          "weekdayName": weekdayParser2(parseDate(Number(yourTime+"01")+i+""))
      });
      //Replacing the original order of the week as Sunday = 0 by Sunday = 7 , then Monday = 1 is the first day
      if (daysOfTheWeeK[i].weekdayNumber === "0") {
          daysOfTheWeeK[i].weekdayNumber = "7";
      }
  };
  daysOfTheWeeK.sort(function(a, b) {
      return parseFloat(a.weekdayNumber) - parseFloat(b.weekdayNumber);
  });