[ IOS ] invalid Date ๊ด€๋ จ ๋ฌธ์ œ

    ๋ฐ˜์‘ํ˜•

    ์ตœ๊ทผ IOS ๋ชจ๋ฐ”์ผ ์•ฑ WebView ์ƒํ™ฉ์—์„œ invalid Date ๋ฌธ์ œ๋ฅผ ๊ฒช๊ฒŒ ๋˜์—ˆ๋‹ค.

    ์ฆ์ƒ์€, ๋‚ ์งœ ๊ฐ์ฒด๊ฐ€ ํ‘œํ˜„์ด ๋˜์ง€ ์•Š๋Š” ํ˜„์ƒ์ด๋ฉฐ, format์„ ๋ณ€ํ™˜ํ•˜๊ฑฐ๋‚˜ date ๊ณ„์‚ฐ์„ ํ•˜๋Š” ๊ณผ์ •์—์„œ ํ•ด๋‹น ํ˜„์ƒ์ด ๋ฐœ์ƒ๋˜์—ˆ๋‹ค. ์šฐ์„ ์ ์œผ๋กœ, IOS์—์„œ๋Š” ISO8601 ํ˜•์‹์„ ๋”ฐ๋ผ์•ผ๋งŒ ํ‘œํ˜„์ด ๊ฐ€๋Šฅํ•˜๋‹ค. 

    https://ko.wikipedia.org/wiki/ISO_8601

     

    ISO 8601 - ์œ„ํ‚ค๋ฐฑ๊ณผ, ์šฐ๋ฆฌ ๋ชจ๋‘์˜ ๋ฐฑ๊ณผ์‚ฌ์ „

    ISO 8601 Data elements and interchange formats - Information interchange - Representation of dates and times์€ ๋‚ ์งœ์™€ ์‹œ๊ฐ„๊ณผ ๊ด€๋ จ๋œ ๋ฐ์ดํ„ฐ ๊ตํ™˜์„ ๋‹ค๋ฃจ๋Š” ๊ตญ์ œ ํ‘œ์ค€์ด๋‹ค. ์ด ํ‘œ์ค€์€ ๊ตญ์ œ ํ‘œ์ค€ํ™” ๊ธฐ๊ตฌ(ISO)์— ์˜ํ•ด ๊ณตํฌ๋˜

    ko.wikipedia.org

     

    ๋”ฐ๋ผ์„œ, ๊ธฐ์กด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๋ฌธ์ž์—ด ํ˜•์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๋˜ Date ๊ฐ์ฒด๋“ค์„ ์ „๋ถ€ ํ•ด๋‹น ํ˜•์‹์œผ๋กœ ๋ฐ”๊พธ์–ด์ฃผ์–ด์•ผ ํ–ˆ๋‹ค. 

    ๊ฒ€์ƒ‰ํ•ด๋ณด๋ฉด moment ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ถ”์ฒœํ•˜๊ณ  ์žˆ์ง€๋งŒ, 2022๋…„ 3์›” ๊ธฐ์ค€ ํ•ด๋‹น ๋ฐฉ๋ฒ•์€ ์†Œ์šฉ์—†์—ˆ๋‹ค. (๋ชจ๋ฐ”์ผ IOS 15๋ฒ„์ „ ์ด์ƒ ๊ธฐ์ค€)

    - moment ์‚ฌ์šฉ (์‹คํŒจ)

    moment(new Date()).format('YYYY.MM.DD');

     

    ์šฐ์„ , IOS์—์„œ ๋™์ž‘ํ•˜๋Š” ๊ฐ์ฒด ํ˜•ํƒœ๋ฅผ ํ™•์ธํ•œ ๊ฒฐ๊ณผ, ๋‚ ์งœ ํ˜•์‹ ์ง€์ • ๋ฐ ๊ณ„์‚ฐ์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•ญ์ƒ ๊ฐ์ฒด๊ฐ€ javascript date ๊ฐ์ฒด์—ฌ์•ผ ํ–ˆ๋‹ค. ๋”ฐ๋ผ์„œ, date ๊ฐ์ฒด๋ฅผ ํ‘œํ˜„ํ•˜๊ฑฐ๋‚˜ ๊ณ„์‚ฐํ•  ๋•Œ๋Š” ํ•ญ์ƒ new Date ๋กœ ์„ ์–ธํ•ด์ฃผ์–ด์•ผ ํ–ˆ๋‹ค.

    - IOS ๋‚ด์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ํ˜•ํƒœ

    new Date()
    new Date(year, month, date, hour, minute, second);
    ...

     

    ์ด๋Ÿฐ ์‹์ด๋‹ค ๋ณด๋‹ˆ ๊ฒฐ๊ตญ ๊ณตํ†ต์œผ๋กœ ๋ฌธ์ž์—ด ํŒจํ„ด์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์•„ date ๊ฐ์ฒด๋กœ ๋ฆฌํ„ดํ•ด์ฃผ๋Š” ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ–ˆ๋‹ค. ๋˜ํ•œ, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ๋Š” UTC ๊ธฐ์ค€์‹œ๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ์–ด์•ผ ํ•˜๋Š” ๋ถ€๋ถ„์ด ์žˆ์–ด ์ด ๋˜ํ•œ ์ ์šฉํ•˜์—ฌ ํ•จ์ˆ˜๋ฅผ ์งฐ๋‹ค.

     

    - ๋ฌธ์ž์—ด์„ ๋ฐ›์•„ javascript date ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜

    initDate: function (date) {
        // ํŒจํ„ด์œผ๋กœ ์ •์˜๋˜๋Š” ์‹œ๊ฐ„ ๋ฐ ๋‚ ์งœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„ Javascript Date Utc ๊ฐ์ฒด๋กœ ๋‚ด๋ณด๋‚ธ๋‹ค.
        var pattern1 = /^(19[0-9]{2}|2[0-9]{3}).(0[1-9]|1[012]).([123]0|[012][1-9]|31) ([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/; // yyyy.mm.dd hh:mm:ss
        var pattern2 = /^(19[0-9]{2}|2[0-9]{3}).(0[1-9]|1[012]).([123]0|[012][1-9]|31)$/; // yyyy.mm.dd
        var pattern3 = /^(0[0-9]|1[0-9]|2[0-3])(:[0-5]\d)(:[0-5]\d)$/; // HH:MM:SS
        var pattern4 = /^([0-5]\d)(:[0-5]\d)$/; // HH:MM
        var pattern5 = /^(19[0-9]{2}|2[0-9]{3})-(0[1-9]|1[012])-([123]0|[012][1-9]|31)$/; // yyyy-mm-dd
        var pattern5_ = /^(19[0-9]{2}|2[0-9]{3})-([1-9])-([1-9]|[123]0|[012][1-9]|31)$/; // yyyy-m-d
        var pattern5__ = /^(19[0-9]{2}|2[0-9]{3})-([1-9])-([123]0|[012][1-9]|31)$/; // yyyy-m-dd
        var pattern5___ = /^(19[0-9]{2}|2[0-9]{3})-(0[1-9]|1[012])-([1-9])$/; // yyyy-mm-d
        var pattern6 = /^(19[0-9]{2}|2[0-9]{3}).(0[1-9]|1[012])$/; // yyyy.mm
        var pattern7 = /^(19[0-9]{2}|2[0-9]{3})-(0[1-9]|1[012])-([123]0|[012][1-9]|31)T([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9].[0-9][0-9][0-9]Z)$/; // IOS yyyy-mm-ddThh:mm:ss.000Z
        var y, m, d, h, mm, s;
    
        if (date == null || date == "" || date == "undefined") return null;
    
        if (date.match(pattern1)) {
          //console.log("* init Date pattern 1 *");
          y = date.substring(0, 4);
          m = date.substring(5, 7);
          d = date.substring(8, 10);
          h = date.substring(11, 13);
          mm = date.substring(14, 16);
          s = date.substring(17, 19);
          return new Date(Date.UTC(y, m - 1, d, h, mm, s));
        } else if (date.match(pattern2)) {
          //console.log("* init Date pattern 2 *");
          y = date.substring(0, 4);
          m = date.substring(5, 7);
          d = date.substring(8, 10);
          h = 0;
          mm = 0;
          s = 0;
          return new Date(Date.UTC(y, m - 1, d, h, mm, s));
        } else if (date.match(pattern3)) {
          //console.log("* init Date pattern 3 *");
          var today = new Date();
          y = today.getFullYear();
          m = today.getMonth();
          d = today.getDate();
          h = date.substring(0, 2);
          mm = date.substring(3, 5);
          s = date.substring(6, 8);
          return new Date(Date.UTC(y, m, d, h, mm, s));
        } else if (date.match(pattern4)) {
          //console.log("* init Date pattern 4 *");
          var today = new Date();
          y = today.getFullYear();
          m = today.getMonth();
          d = today.getDate();
          h = date.substring(0, 2);
          mm = date.substring(3, 5);
          s = 0;
          return new Date(Date.UTC(y, m, d, h, mm, s));
        } else if (date.match(pattern5)) {
          //console.log("* init Date pattern 5 *");
          var today = new Date();
          y = date.substring(0, 4);
          m = date.substring(5, 7);
          d = date.substring(8, 10);
          h = 0;
          mm = 0;
          s = 0;
          return new Date(Date.UTC(y, m, d, h, mm, s));
        } else if (date.match(pattern5_)) {
          //console.log("* init Date pattern 5_ *");
          var today = new Date();
          y = date.substring(0, 4);
          m = date.substring(5, 6);
          d = date.substring(7, 8);
          h = 0;
          mm = 0;
          s = 0;
          console.log(y, m, d, h, mm, s);
          return new Date(Date.UTC(y, m, d, h, mm, s));
        } else if (date.match(pattern5__)) {
          //console.log("* init Date pattern 5__ *");
          var today = new Date();
          y = date.substring(0, 4);
          m = date.substring(5, 6);
          d = date.substring(7, 9);
          h = 0;
          mm = 0;
          s = 0;
          return new Date(Date.UTC(y, m, d, h, mm, s));
        } else if (date.match(pattern5___)) {
          //console.log("* init Date pattern 5___ *");
          var today = new Date();
          y = date.substring(0, 4);
          m = date.substring(5, 7);
          d = date.substring(8, 9);
          h = 0;
          mm = 0;
          s = 0;
          return new Date(Date.UTC(y, m, d, h, mm, s));
        } else if (date.match(pattern6)) {
          //console.log("* init Date pattern 6 *");
          var today = new Date();
          y = date.substring(0, 4);
          m = date.substring(5, 7);
          d = 1;
          h = 0;
          mm = 0;
          s = 0;
          return new Date(Date.UTC(y, m - 1, d, h, mm, s));
        } else if (date.match(pattern7)) {
          //console.log("* init Date pattern 6 *");
          /* 2022-02-16T10:28:00.000Z */
          y = date.substring(0, 4);
          m = date.substring(5, 7);
          d = date.substring(8, 10);
          h = date.substring(11, 13);
          mm = date.substring(14, 16);
          s = date.substring(17, 19);
          return new Date(Date.UTC(y, m - 1, d, h, mm, s));
        } else {
          //console.log("* init Date no match *");
          return null;
        }
      }

     

    ์ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ, ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ด๋ฐ›์€ ํ›„ ํฌ๋งท ๋ณ€๊ฒฝ์ด๋‚˜ ๊ธฐํƒ€ ์ž‘์—…์„ ํ–ˆ๋‹ค. ๋ฌธ์ œ๋Š” ํ•ด๋‹น ํ˜•์‹์ด ํ•„์š”ํ•œ๊ฒŒ IOS ๋ชจ๋ฐ”์ผ ํ™˜๊ฒฝ์—์„œ๋งŒ ๊ทธ๋žฌ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

    ์ •๋ง ๋ถˆํŽธํ•˜๊ธด ํ•˜์ง€๋งŒ ์ด๋Ÿฐ ๋ถ€๋ถ„์ด ์•„๋‹ˆ๋ฉด ์ธ์‹์„ ํ•˜์ง€ ๋ชปํ•˜๋‹ˆ ์šธ๋ฉฐ ๊ฒจ์ž๋จน๊ธฐ๋กœ ์ผ์ผํžˆ ๋ณ€๊ฒฝ์„ ํ•ด์ฃผ์–ด์•ผ ํ–ˆ๋‹ค.

     

    728x90
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€