Last active
February 17, 2023 09:54
-
-
Save khanhkhuu/94556057bde96108dc77d2264b9316b8 to your computer and use it in GitHub Desktop.
Convert Thai date to International format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For example: 4 ต.ค. 65 | |
// Or: 9 กุมภาพันธ์ 2566 | |
function convertToIntDate(thaiDate) { | |
const dateFormat = 'dd-MM-yyyy'; | |
if (thaiDate instanceof Date) { | |
const newDate = new Date( | |
thaiDate.getFullYear() + 57, | |
thaiDate.getMonth(), | |
thaiDate.getDate() | |
); | |
return Utilities.formatDate(newDate, 'Asia/Bangkok', dateFormat) | |
} | |
const parts = thaiDate.split(' '); | |
const monthMapper = { | |
'ม.ค.': 1, | |
'ก.พ.': 2, | |
'มี.ค.': 3, | |
'เม.ย.': 4, | |
'พ.ค.': 5, | |
'มิ.ย.': 6, | |
'ก.ค.': 7, | |
'ส.ค.': 8, | |
'ก.ย.': 9, | |
'ต.ค.': 10, | |
'พ.ย.': 11, | |
'ธ.ค.': 12, | |
'มกราคม': 1, | |
'กุมภาพันธ์': 2, | |
'มีนาคม': 3, | |
'เมษายน': 4, | |
'พฤษภาคม': 5, | |
'มิถุนายน': 6, | |
'กรกฎาคม': 7, | |
'สิงหาคม': 8, | |
'กันยายน': 9, | |
'ตุลาคม': 10, | |
'พฤศจิกายน': 11, | |
'ธันวาคม': 12, | |
'เดือนมกราคม': 1, | |
'เดือนกุมภาพันธ์': 2, | |
'เดือนมีนาคม': 3, | |
'เดือนเมษายน': 4, | |
'เดือนพฤษภาคม': 5, | |
'เดือนมิถุนายน': 6, | |
'เดือนกรกฎาคม': 7, | |
'เดือนสิงหาคม': 8, | |
'เดือนกันยายน': 9, | |
'เดือนตุลาคม': 10, | |
'เดือนพฤศจิกายน': 11, | |
'เดือนธันวาคม': 12 | |
} | |
const day = Number(parts[0]); | |
const month = monthMapper[parts[1]] - 1; | |
let year = Number(parts[2]); | |
if (year < 100) year = year + 2500; | |
year = year - 543; | |
return Utilities.formatDate(new Date(year, month, day), 'Asia/Bangkok', dateFormat) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment