Giter Site home page Giter Site logo

datepicker-js-thai-year's Introduction

Datepicker-js-Thai-year

Date Picker เปลี่ยนจากปี ค.ศ เป็น พ.ศ.

วัตถุประสงค์

เนื่องจากโปรเจ็คที่ทำผู้ใช้งานต้องการใช้ภาษาไทยทั้งหมด รวมทั้งหน่วยและวันเดือนปีต่างๆ ด้วย ในส่วนของ วันเดือนปี ผมได้เลือกให้ JQuery library date picker ตัวนี้ pickadate.js. เพราะดีไซล์ทันสมัย, responsive, ขนาดเล็กเบา.

Dependencies

การติดตั้ง

<!-- JQuery -->
<script src="Your_Path_To/jquery-3.7.0.min.js"></script>

<!-- Pickadate -->
<!-- CSS -->
    <link rel="stylesheet" href="Your_Path_To/pickadate/lib/themes/default.css">
    <link rel="stylesheet" href="Your_Path_To/pickadate/lib/themes/default.date.css">

<!-- JavaScript -->
    <script src="Your_Path_To/pickadate/lib/picker.js"></script>
    <script src="Your_Path_To/pickadate/lib/picker.date.js"></script>

การเรียกใช้

<script>
    $('.datepicker').pickadate({
        max: true, //ไม่สามารถเลือกวันเดือนปี เกินวันที่ปัจจุบันได้
        monthsFull: ['มกราคม', 'กุมภาพันธ์', 'มีนาคม', 'เมษายน', 'พฤษภาคม', 'มิถุนายน', 'กรกฎาคม', 'สิงหาคม', 'กันยายน', 'ตุลาคม', 'พฤศจิกายน', 'ธันวาคม'],
        monthsShort: ['ม.ค.', 'ก.พ.', 'มี.ค.', 'เม.ย.', 'พ.ค.', 'มิ.ย.', 'ก.ค.', 'ส.ค.', 'ก.ย.', 'ต.ค.', 'พ.ย.', 'ธ.ค.'],
        weekdaysFull: ['อาทิตย์', 'จันทร์', 'อังคาร', 'พุธ', 'พฤหัสบดี', 'ศุกร์', 'เสาร์'],
        weekdaysShort: ['อา.', 'จ.', 'อ.', 'พ.', 'พฤ.', 'ศ.', 'ส.'],
        today: 'วันนี้',
        clear: 'ลบ',
        format: 'dd-mm-yyyy',
        formatSubmit: 'dd-mm-yyyy',
        selectMonths: true, //เลือกเดือนได้
        selectYears: 5, //เลือกปีได้ 5ปี (true, ค่าเริ่มต้น 10ปี )
        onRender: function () {
            var yearDropdown = this.$root.find('.picker__select--year');
            if (yearDropdown.length > 0) {
                yearDropdown.find('option').each(function () {
                    var westernYear = parseInt($(this).text());
                    var buddhistYear = westernYear + 543; // แปลงเป็น พ.ศ.
                    $(this).text(buddhistYear);
                });
            }
        },
        onClose: function () {
            // จัดรูปแบบค่าฟิลด์อินพุตหลังจากที่ตัวเลือกปิด
            var selectedDate = this.get('select', 'dd-mm-yyyy'); // รับวันที่เลือก
            var parts = selectedDate.split('-'); // แยกส่วนวันที่
            var buddhistYear = parseInt(parts[2]) + 543; // แปลงเป็น พ.ศ. (เนื่องจากค่าใน input จะยังเป็น ค.ศ. จึงต้องแปลงอีกรอบ)
            var formattedDate = parts[0] + '-' + parts[1] + '-' + buddhistYear; // จัดวันที่ใหม่
            this.$node.val(formattedDate); // อัปเดตค่าอินพุต
        }
    });
    
</script>

หมายเหตุ

สำหรับฐานข้อมูล ส่วนตัวแล้ว ฟิลต์วันเดือนปี ผมจะเก็บในรูปแบบตัวอักษรนะครับ varchar(10) dd-mm-yyyy (เฉพาะโปรเจ็คที่ต้องการรูปแบบไทยแบบนี้ครับ) เพราะสามารถเรียกมาแสดงผลได้โดยตรงไม่ต้องแปลงอีกทีครับ. อีกส่วนที่ผมใช้คือ เอามาคำนวนอายุก็ยังสามารถทำได้ไม่มีปัญหาอะไรครับ. ขอบคุณครับ

datepicker-js-thai-year's People

Contributors

panumas77 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.