์ต๊ทผ 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 ๋ชจ๋ฐ์ผ ํ๊ฒฝ์์๋ง ๊ทธ๋ฌ๋ค๋ ๊ฒ์ด๋ค.
์ ๋ง ๋ถํธํ๊ธด ํ์ง๋ง ์ด๋ฐ ๋ถ๋ถ์ด ์๋๋ฉด ์ธ์์ ํ์ง ๋ชปํ๋ ์ธ๋ฉฐ ๊ฒจ์๋จน๊ธฐ๋ก ์ผ์ผํ ๋ณ๊ฒฝ์ ํด์ฃผ์ด์ผ ํ๋ค.
'๐JS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ToastUI Editor] ์ด๋ฏธ์ง ๋ณต์ฌ ๋ถ์ฌ ๋ฃ๊ธฐ ์, ํ์ผ ์ ๋ก๋ ๋ฐ UI์ฒ๋ฆฌ (0) | 2022.03.29 |
---|---|
xlsx-js-style ๋ฅผ ์ด์ฉํ table ์์๋ก ์์ ํ์ผ ๋ค์ด๋ก๋ ์ํค๊ธฐ (1) | 2021.11.05 |
[Javascript] Promise ์ดํดํ๊ธฐ (0) | 2021.10.06 |
String Array ๋ด ๋น ์์ ์ ๊ฑฐํ๊ธฐ (0) | 2021.02.06 |
์คํฌ๋ฆฝํธ ํ์ผ import ์, ์บ์ ์๋จ๊ฒ ์ฒ๋ฆฌํ๊ธฐ (0) | 2021.02.06 |
๋๊ธ