Convert string YYMMDD to YYYYMMDD in JavaScript - Stack Overflow

I am just wondering is it possible to convert a date string YYMMDD to YYYY-MM-DD, in example 990102 -&g

I am just wondering is it possible to convert a date string YYMMDD to YYYY-MM-DD, in example 990102 -> 1999-01-02 in JavaScript?

EDIT from a ment :

If date 200102 is provided it need to output 2012-01-02, not 1920-01-02

I am just wondering is it possible to convert a date string YYMMDD to YYYY-MM-DD, in example 990102 -> 1999-01-02 in JavaScript?

EDIT from a ment :

If date 200102 is provided it need to output 2012-01-02, not 1920-01-02

Share Improve this question edited May 9, 2017 at 11:07 Mihai Alexandru-Ionut 48.5k14 gold badges105 silver badges132 bronze badges asked May 9, 2017 at 9:50 StreemStreem 6282 gold badges14 silver badges26 bronze badges 5
  • 1 You cannot unless you provide a condition on when to use 2000+ and when to use 1900+. Its basically less information in YY where the thousands and hundreds are missing. There is no obvious way to determine if 14 is 2014 or 1914 – sabithpocker Commented May 9, 2017 at 9:56
  • So you can take a window like [1949 - 2050]. So if YY>=49 use 1900+. If YY<50 use 2000+ The window can be adjusted per your requirement – sabithpocker Commented May 9, 2017 at 10:01
  • In mon use, if YY is greater than a value, it is 1900, if not it is 2000. The value is not constant, depends on the date itself. – AxelH Commented May 9, 2017 at 10:01
  • Yes, that value should be determined per use-case. – sabithpocker Commented May 9, 2017 at 10:10
  • Didn't you mean 'If date 120102 is provided it need to output 2012-01-02, not 1920-01-02'? I don't think so you need something out of range [1970:2038], two last digits are now unique. – ulou Commented May 9, 2017 at 10:35
Add a ment  | 

3 Answers 3

Reset to default 4

You cannot unless you provide a condition on when to use 2000+ and when to use 1900+. Its basically less information in YY where the thousands and hundreds are missing. There is no obvious way to determine if 14 is 2014 or 1914

So you can take a window like [1990 - 2089]. So if YY>=90 use 1900+. If YY<90 use 2000+ The window can be adjusted per your requirement

I am not sure if there is any standard on what window to use.

@Kos says:

There's a POSIX standard: "When a century is not otherwise specified, values in the range [69,99] shall refer to years 1969 to 1999 inclusive, and values in the range [00,68] shall refer to years 2000 to 2068 inclusive"

@RobG says

ECMA-262 (which is probably more appropriate for javascript) says that any year from 0 to 99 should be treated as 1900 to 1999 (§20.3.2.1). But of course implementations can do what they want for formats not covered by the spec. In Safari, 1/1/49 is 2049, 1/1/50 is 1950.

//990102 -> 1999-01-02
//using window [1990-2089]
function convertDate(yymmdd) {
  var d = yymmdd; // YYMMDD
  var yy = d.substr(0, 2);
  var mm = d.substr(2, 2);
  var dd = d.substr(4, 2);
  var yyyy = (+yy < 90) ? '20' + yy : '19' + yy;
  return yyyy + '-' + mm + '-' + dd;
}
console.log(convertDate('900102'))
console.log(convertDate('140102'))
console.log(convertDate('890102'))

You can use substring method.

var date="990102";
console.log("19"+date.substring(0,2)+"-"+date.substring(2,4)+"-"+date.substring(4,6));

I know this is an old question but here is my solution (in typescript and it worked for me)

private convertDate(yymmdd: string) {

    const date: Date = new Date();
    const year = date.getFullYear().toString();

    const d = yymmdd; // YYMMDD
    const yy = d.substr(0, 2);
    const mm = d.substr(2, 2);
    const dd = d.substr(4, 2);
    const yyyy = (+yy < parseInt(year.slice(2, year.length), 10) + 10) ? '20' + yy : '19' + yy;

    return `${yyyy}-${mm}-${dd}`;
}

It is an adaptation of the answer above except it ensures the gap remains at an incremental (10) distance from the current year to ensure the sustainability

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744316887a4568227.html

相关推荐

  • Convert string YYMMDD to YYYYMMDD in JavaScript - Stack Overflow

    I am just wondering is it possible to convert a date string YYMMDD to YYYY-MM-DD, in example 990102 -&g

    7天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信