简体   繁体   English

将数字DDMMYY转换为日期?

[英]Convert number DDMMYY into date?

I am new to JavaScript (but not programming) and am having a difficult time figuring out where I made a mistake in this function, found here: http://mikeryan.webatu.com/function.html [dead link][Guessing of the original code at the bottom] 我是JavaScript的新手(但不是编程人员),并且很难找出我在此函数中的错误之处,可以在以下位置找到: http : //mikeryan.webatu.com/function.html [死链接] [猜测底部的原始代码]

The function should take a DDMMYY timestamp and convert it into a human-readable string . 该函数应采用DDMMYY时间戳并将其转换为人类可读的string For instance, 210710 would be turned into July 21st, 2010 . 例如, 210710将变成July 21st, 2010

UPDATE: Code that is probably similar OP's dead link: 更新:可能与OP的死链接类似的代码:

function timestamp(d){
    var year = (d-(Math.round(d / 100)*100);

    var day = Math.floor(d/10000);
    var dayfix = (day - (Math.floor(day/10)*10));

    // var month = ((d-year)-(day*100000)/100);

    var a = (d - year);
    var b = ((day * 100000) / 10);

    var month = (a - b) / 100;

    var months = new Array();
    months[1]  = "January";
    months[2]  = "February";
    months[3]  = "March";
    months[4]  = "April";
    months[5]  = "May";
    months[6]  = "June";
    months[7]  = "July";
    months[8]  = "August";
    months[9]  = "September";
    months[10]  = "October";
    months[11] = "November";
    months[12] = "December";

    var daysuffix = new Array();
    daysuffix[0] = "th";
    daysuffix[1] = "st";
    daysuffix[2] = "nd";
    daysuffix[3] = "rd";
    daysuffix[4] = "th";
    daysuffix[5] = "th";
    daysuffix[6] = "th";
    daysuffix[7] = "th";
    daysuffix[8] = "th";
    daysuffix[9] = "th";

    if(year>20){
       year = '19' + year;
    }
    else{
       year = '20' + year;
    }
    return (months[month] + ' ' + day + daysuffix[dayfix] + ', ' + year);
 }

One problem: you're missing a parenthesis. 一个问题:您缺少括号。 Change: 更改:

var year = (d-(Math.round(d / 100)*100);

to

var year = (d-(Math.round(d / 100)*100));

That being said, this is a more straightforward calculation method: 话虽如此,这是一种更直接的计算方法:

var year = d % 100;
var month = Math.floor(d / 100) % 100;
var day = Math.floor(d / 10000) % 100;

Next, your array initialization is unnecessarily verbose. 接下来,数组初始化不必要地冗长。 Instead of: 代替:

var arr = new Array();
arr[0] = "foo";
arr[1] = "bar";

just do: 做就是了:

var arr = ["foo", "bar"];

Your day suffix is incorrect. 您的日后缀不正确。 It puts "nd" after 12 and "12nd April" clearly isn't correct. 它将“ nd”放在12月12日之后,“ 4月12日”显然是不正确的。 I would just use logic for doing this rather than a lookup array where most elements are "th". 我只会使用逻辑来执行此操作,而不是使用大多数元素都是“ th”的查找数组。

So: 所以:

function timestamp(d){
  var year = d % 100;
  var month = Math.floor(d / 100) % 100;
  var day = Math.floor(d / 10000) % 100;
  var months = ["January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"];
  if (year>20) {
    year = '19' + year;
  } else {
    year = '20' + year;
  }
  if (day == 1 || day == 21 || day == 31) {
    var suffix = "st";
  } else if (day == 2 || day == 22) {
    var suffix = "nd";
  } else {
    var suffix = "th";
  }
  return (months[month-1] + ' ' + day + suffix + ', ' + year);
}

Lastly there is little value in your "timestamp" being an integer in its present form. 最后,您的“时间戳”几乎没有任何值,它是当前形式的整数。 A more typical format for tis kind of thing is YYYYMMDD for two reasons: 这类事情更典型的格式是YYYYMMDD,这有两个原因:

  1. Numerical ordering matches date ordering; 数字排序与日期排序匹配; and

  2. It's unambiguous. 这是明确的。 North Americans put month before day (ie MMDDYY). 北美人日复一日地投放广告(例如MMDDYY)。 Everyone else in the world puts day first (ie DDMMYY). 世界上其他所有人都把第一天(即DDMMYY)放在第一位。 No one does YYDDMM. 没有人做YYDDMM。

I'd use % - modulo: X modulo 100 discards anything except the last 2 digits. 我会使用% -模数:X模数100会丢弃除最后两位以外的所有内容。 Useful! 有用!

also use floor not round 也用floorround

Use the date object. 使用日期对象。 It's a whole lot faster. 快很多了。 Here is a quick example it assumes the year is going to be in the 2000s so you would have to do some modifications. 这是一个简单的示例,它假设年份将在2000年代,因此您必须进行一些修改。 And the output is not exactly what you have, but it is pretty close and the code is a whole lot shorter. 输出并不完全是您所拥有的,但是非常接近,并且代码短了很多。

function date(e){
  var d = new Date();
  d.setYear(2000+e.substring(4)/1,e.substring(2,4)-1,e.substring(0,2)-1);
  alert(d.toDateString());
}

First use Math.floor to get the floor value of the decimal, then there were some typos in your function. 首先使用Math.floor获取小数的下限值,然后在函数中输入一些错字。 Here is the code that works (Note: just tested with a couple of examples) but should be enough to get your started: 这是有效的代码(注意:仅通过几个示例进行了测试),但足以开始使用:

          function timestamp(d){
        var year = (d-(Math.floor(d / 100)*100));

        var day = Math.floor(d/10000);
        var dayfix = (day - (Math.floor(day/10)*10));

        // var month = ((d-year)-(day*100000)/100);

        var a = (d - year);
        var b = ((day * 100000) / 10);

        var month = (a - b) / 100;

        var months = new Array();
        months[1]  = "January";
        months[2]  = "February";
        months[3]  = "March";
        months[4]  = "April";
        months[5]  = "May";
        months[6]  = "June";
        months[7]  = "July";
        months[8]  = "August";
        months[9]  = "September";
        months[10]  = "October";
        months[11] = "November";
        months[12] = "December";

        var daysuffix = new Array();
        daysuffix[0] = "th";
        daysuffix[1] = "st";
        daysuffix[2] = "nd";
        daysuffix[3] = "rd";
        daysuffix[4] = "th";
        daysuffix[5] = "th";
        daysuffix[6] = "th";
        daysuffix[7] = "th";
        daysuffix[8] = "th";
        daysuffix[9] = "th";

        if(year>20){
           year = '19' + year;
        }
        else{
           year = '20' + year;
        }
        return (months[month] + ' ' + day + daysuffix[dayfix] + ', ' + year);
     }

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

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