วิธีรับวันที่ปัจจุบันใน jquery


181

ฉันต้องการทราบวิธีใช้ฟังก์ชัน Date () ใน jQuery เพื่อรับวันที่ปัจจุบันในyyyy/mm/ddรูปแบบ

คำตอบ:


325

Date()ไม่ได้เป็นส่วนหนึ่งของjQueryมันเป็นหนึ่งในคุณสมบัติของ JavaScript

ดูเอกสารเกี่ยวกับวัตถุวัน

คุณสามารถทำเช่นนั้นได้:

var d = new Date();

var month = d.getMonth()+1;
var day = d.getDate();

var output = d.getFullYear() + '/' +
    (month<10 ? '0' : '') + month + '/' +
    (day<10 ? '0' : '') + day;

ดูjsfiddle นี้เพื่อพิสูจน์

รหัสอาจดูเหมือนซับซ้อนเนื่องจากต้องจัดการกับเดือน & วันที่แสดงโดยตัวเลขน้อยกว่า10(หมายถึงสตริงจะมีอักขระหนึ่งตัวแทนที่จะเป็นสองตัว) ดูjsfiddle นี้เพื่อเปรียบเทียบ


2
ฉันไม่รู้ ฉันกำลังค้นหาที่จะได้รับวันที่ปัจจุบันในความช่วยเหลือ jquery.plz
ซาร่า

ขอบคุณสำหรับคำตอบ :) แม้ว่าฉันจะไม่เคยเห็นการจัดรูปแบบวันที่เช่น yyyy / mm / dd - มันถูกใช้ในบางประเทศหรือไม่? ฉันเห็น yyyy-mm-dd และ yyyymmdd แล้ว
Manachi

1
@Manachi ใช่มันใช้ในศรีลังกา
Sara

2
ยังใช้ในปาเลสไตน์ :)
Nada N. Hantouli

ยังใช้กันอย่างแพร่หลายในเกาหลีและแอฟริกาใต้
Muleskinner

131

หากคุณมี jQuery UI (จำเป็นสำหรับ datepicker) สิ่งนี้จะทำเคล็ดลับ:

$.datepicker.formatDate('yy/mm/dd', new Date());

6
ต้องการ jquery-ui
Alex G

1
ใช่. ฉันใช้ jQuery UI ดังนั้นโซลูชันนี้จึงเหมาะสำหรับฉัน ขอบคุณ
Chen Li Yong

44

jQuery คือ JavaScript ใช้DateวัตถุJavascript

var d = new Date();
var strDate = d.getFullYear() + "/" + (d.getMonth()+1) + "/" + d.getDate();

3
d.getMonth () ส่งคืนเดือน (จาก 0-11) ดังนั้นจึงอาจผิด
Gaurav Agrawal

2
getMonth()ตัวเลขผลตอบแทนระหว่างและ0 11นี่เป็นข้อผิดพลาดทั่วไปของ JavaScript นอกจากนี้ยังtoString()ทำงานในลักษณะที่แตกต่างกันกว่าที่คุณอธิบาย (ดูjsfiddle นี้และหน้าเอกสารนี้ ) เพื่อสรุป: ไม่มีวิธีแก้ไขปัญหาใดที่คุณจัดหามาให้อย่างเหมาะสม
Tadeck

1
เดือนที่ฉันไม่มีข้อแก้ตัวผิดพลาดที่ฉันเคยทำมาก่อนและไม่ได้เรียนรู้จากมัน! toStringแม้ว่าฉันจะสาบานได้ แต่ฉันทดสอบ jsFiddle ของคุณด้วย Chrome และคุณพูดถูก ลบออกจากคำตอบของฉัน ขอบคุณ
Connell

31

การใช้ Javascript บริสุทธิ์ของคุณสามารถสร้างต้นแบบรูปแบบ YYYYMMDD ของคุณเอง ;

Date.prototype.yyyymmdd = function() {
  var yyyy = this.getFullYear().toString();
  var mm = (this.getMonth()+1).toString(); // getMonth() is zero-based
  var dd  = this.getDate().toString();
  return yyyy + "/" + (mm[1]?mm:"0"+mm[0]) + "/" + (dd[1]?dd:"0"+dd[0]); // padding
};

var date = new Date();
console.log( date.yyyymmdd() ); // Assuming you have an open console

21

ใน JavaScript คุณสามารถรับวันที่และเวลาปัจจุบันโดยใช้วัตถุ Date;

var now = new Date();

นี่จะเป็นเวลาของเครื่องไคลเอ็นต์โลคัล

ตัวอย่างสำหรับ jquery LINK

หากคุณใช้ jQuery DatePicker คุณสามารถนำไปใช้กับ textfield เช่นนี้:

$( "#datepicker" ).datepicker({dateFormat:"yy/mm/dd"}).datepicker("setDate",new Date());

นี่จะได้ค่าวันที่เต็มเวลาไม่ใช่สิ่งที่ฉันต้องการ
ซาร่า

15

เนื่องจากคำถามถูกแท็กเป็นJQuery:

หากคุณกำลังใช้อยู่JQuery UIคุณสามารถใช้$.datepicker.formatDate():

$.datepicker.formatDate('yy/mm/dd', new Date());

ดูการสาธิตนี้


15
function GetTodayDate() {
   var tdate = new Date();
   var dd = tdate.getDate(); //yields day
   var MM = tdate.getMonth(); //yields month
   var yyyy = tdate.getFullYear(); //yields year
   var currentDate= dd + "-" +( MM+1) + "-" + yyyy;

   return currentDate;
}

ฟังก์ชั่นที่ใช้งานง่ายและสนุก


2
สิ่งนี้ทำงานได้อย่างสวยงามในการตั้งค่าและจากนั้นการโทรตามลำดับเพื่อซ่อนตัวใช้เลือกวันที่จะแก้ไขค่าในเบราว์เซอร์ทุกตัว เรียบร้อย!
nicholeous


9

นี่คือวิธีการด้านบนรับวันปัจจุบันปีหรือเดือน

new Date().getDate()          // Get the day as a number (1-31)
new Date().getDay()           // Get the weekday as a number (0-6)
new Date().getFullYear()      // Get the four digit year (yyyy)
new Date().getHours()         // Get the hour (0-23)
new Date().getMilliseconds()  // Get the milliseconds (0-999)
new Date().getMinutes()       // Get the minutes (0-59)
new Date().getMonth()         // Get the month (0-11)
new Date().getSeconds()       // Get the seconds (0-59)
new Date().getTime()          // Get the time (milliseconds since January 1, 1970)


6
//convert month to 2 digits<p>
var twoDigitMonth = ((fullDate.getMonth().length+1) === 1)? (fullDate.getMonth()+1) : '0' + (fullDate.getMonth()+1);

var currentDate =  fullDate.getFullYear()+ "/" + twoDigitMonth + "/" + fullDate.getDate();
console.log(currentDate);<br>
//2011/05/19

6

วัตถุนี้ตั้งค่าเป็นศูนย์เมื่อองค์ประกอบมีสัญลักษณ์เดียวเท่านั้น:

function addZero(i) {
    if (i < 10) {
        i = "0" + i;
    }
    return i;
}

วัตถุนี้ตั้งค่าเต็มเวลาจริงชั่วโมงและวันที่:

function getActualFullDate() {
    var d = new Date();
    var day = addZero(d.getDate());
    var month = addZero(d.getMonth()+1);
    var year = addZero(d.getFullYear());
    var h = addZero(d.getHours());
    var m = addZero(d.getMinutes());
    var s = addZero(d.getSeconds());
    return day + ". " + month + ". " + year + " (" + h + ":" + m + ")";
}

function getActualHour() {
    var d = new Date();
    var h = addZero(d.getHours());
    var m = addZero(d.getMinutes());
    var s = addZero(d.getSeconds());
    return h + ":" + m + ":" + s;
}

function getActualDate() {
    var d = new Date();
    var day = addZero(d.getDate());
    var month = addZero(d.getMonth()+1);
    var year = addZero(d.getFullYear());
    return day + ". " + month + ". " + year;
}

HTML:

<span id='full'>a</span>
<br>
<span id='hour'>b</span>
<br>    
<span id='date'>c</span>

มุมมอง JQUERY:

$(document).ready(function(){
    $("#full").html(getActualFullDate());
    $("#hour").html(getActualHour());
    $("#date").html(getActualDate());
});

ตัวอย่าง


6

ฉันรู้ว่าฉันมาสาย แต่นี่คือทั้งหมดที่คุณต้องการ

var date = (new Date()).toISOString().split('T')[0];

toISOString () ใช้ฟังก์ชั่นที่สร้างขึ้นจากจาวาสคริปต์

cd = (new Date()).toISOString().split('T')[0];
console.log(cd);
alert(cd);


5

คุณสามารถทำสิ่งนี้ได้ด้วย moment.js เช่นกัน รวม moment.js ใน html ของคุณ

<script src="moment.js"></script>

และใช้รหัสด้านล่างในไฟล์สคริปต์เพื่อรับวันที่จัดรูปแบบ

moment(new Date(),"YYYY-MM-DD").utcOffset(0, true).format();


3

FYI - getDay () จะให้วันของสัปดาห์กับคุณ ... เช่น: ถ้าวันนี้เป็นวันพฤหัสบดีก็จะส่งคืนหมายเลข 4 (เป็นวันที่ 4 ของสัปดาห์)

ในการรับวันที่เหมาะสมของเดือนให้ใช้ getDate ()

ตัวอย่างด้านล่างของฉัน ... (เช่นเดียวกับฟังก์ชั่นการขยายสตริงเพื่อให้นำ 0 ในองค์ประกอบเวลาเดียว (เช่น: 10: 4: 34 => 10:04:35)

function strpad00(s)
{
    s = s + '';
    if (s.length === 1) s = '0'+s;
    return s;
}

var currentdate = new Date();
var datetime = currentdate.getDate() 
    + "/" + strpad00((currentdate.getMonth()+1)) 
    + "/" + currentdate.getFullYear() 
    + " @ " 
    + currentdate.getHours() + ":" 
    + strpad00(currentdate.getMinutes()) + ":" 
    + strpad00(currentdate.getSeconds());

ตัวอย่างการส่งออก: 31/12/2013 @ 10:07:49
ถ้าใช้ getDay () การส่งออกจะเป็น4 /12/2013 @ 10:07:49




2
var d = new Date();

var today = d.getFullYear() + '/' + ('0'+(d.getMonth()+1)).slice(-2) + '/' + ('0'+d.getDate()).slice(-2);

ถูกแก้ไขแล้ว d.getMonth () + 1 จะต้องคำนวณ (= กำหนดระหว่างวงเล็บ) ก่อนที่จะเพิ่ม '0' ที่หน้าสตริง ...
Filip


1

คุณสามารถทำได้:

    var now = new Date();
    dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");
     // Saturday, June 9th, 2007, 5:46:21 PM

หรือสิ่งที่ชอบ

    var dateObj = new Date();
    var month = dateObj.getUTCMonth();
    var day = dateObj.getUTCDate();
    var year = dateObj.getUTCFullYear();
    var newdate = month + "/" + day + "/" + year;
    alert(newdate);

1

คุณสามารถใช้รหัสนี้:

var nowDate     = new Date();
var nowDay      = ((nowDate.getDate().toString().length) == 1) ? '0'+(nowDate.getDate()) : (nowDate.getDate());
var nowMonth    = ((nowDate.getMonth().toString().length) == 1) ? '0'+(nowDate.getMonth()+1) : (nowDate.getMonth()+1);
var nowYear     = nowDate.getFullYear();
var formatDate  = nowDay + "." + nowMonth + "." + nowYear;

คุณสามารถค้นหาตัวอย่างการทำงานที่นี่


1

นี่คือสิ่งที่ฉันคิดขึ้นโดยใช้ jQuery เท่านั้น มันเป็นเพียงเรื่องของการรวมชิ้นส่วนเข้าด้วยกัน

        //Gather date information from local system
        var ThisMonth = new Date().getMonth() + 1;
        var ThisDay = new Date().getDate();
        var ThisYear = new Date().getFullYear();
        var ThisDate = ThisMonth.toString() + "/" + ThisDay.toString() + "/" + ThisYear.toString();

        //Gather time information from local system
        var ThisHour = new Date().getHours();
        var ThisMinute = new Date().getMinutes();
        var ThisTime = ThisHour.toString() + ":" + ThisMinute.toString();

        //Concatenate date and time for date-time stamp
        var ThisDateTime = ThisDate  + " " + ThisTime;

0
var d = new Date();
var month = d.getMonth() + 1;
var day = d.getDate();
var year = d.getYear();
var today = (day<10?'0':'')+ day + '/' +(month<10?'0':'')+ month + '/' + year;
alert(today);

นี่เป็นสำเนาของคำตอบที่ได้รับการยอมรับยกเว้นในรูปแบบที่ผิด (d / m / y)
Rob Grzyb

0

ฉันแค่อยากจะแบ่งปันต้นแบบเวลาประทับที่ฉันทำโดยใช้ความคิดของปิแอร์ คะแนนไม่เพียงพอที่จะแสดงความคิดเห็น :(

// US common date timestamp
Date.prototype.timestamp = function() {
  var yyyy = this.getFullYear().toString();
  var mm = (this.getMonth()+1).toString(); // getMonth() is zero-based
  var dd  = this.getDate().toString();
  var h = this.getHours().toString();
  var m = this.getMinutes().toString();
  var s = this.getSeconds().toString();

  return (mm[1]?mm:"0"+mm[0]) + "/" + (dd[1]?dd:"0"+dd[0]) + "/" + yyyy + " - " + ((h > 12) ? h-12 : h) + ":" + m + ":" + s;
};

d = new Date();

var timestamp = d.timestamp();
// 10/12/2013 - 2:04:19

0

การใช้ jQuery-ui datepicker นั้นมีการแปลงวันที่สะดวกในตัวคุณจึงสามารถจัดรูปแบบวันที่:

var my_date_string = $.datepicker.formatDate( "yy-mm-dd",  new Date() );

ง่าย


0

รับรูปแบบวันที่ปัจจุบัน dd/mm/yyyy

นี่คือรหัส:

var fullDate = new Date();
var twoDigitMonth = ((fullDate.getMonth().toString().length) == 1)? '0'+(fullDate.getMonth()+1) : (fullDate.getMonth()+1);
var twoDigitDate = ((fullDate.getDate().toString().length) == 1)? '0'+(fullDate.getDate()) : (fullDate.getDate());
var currentDate = twoDigitDate + "/" + twoDigitMonth + "/" + fullDate.getFullYear();
alert(currentDate);

0
function createDate() {
            var date    = new Date(),
                yr      = date.getFullYear(),
                month   = date.getMonth()+1,
                day     = date.getDate(),
                todayDate = yr + '-' + month + '-' + day;
            console.log("Today date is :" + todayDate);

0

คุณสามารถเพิ่มวิธีการขยายไปยังจาวาสคริปต์

Date.prototype.today = function () {
    return ((this.getDate() < 10) ? "0" : "") + this.getDate() + "/" + (((this.getMonth() + 1) < 10) ? "0" : "") + (this.getMonth() + 1) + "/" + this.getFullYear();
}

-3
function returnCurrentDate() {
                var twoDigitMonth = ((fullDate.getMonth().toString().length) == 1) ? '0' + (fullDate.getMonth() + 1) : (fullDate.getMonth() + 1);
                var twoDigitDate = ((fullDate.getDate().toString().length) == 1) ? '0' + (fullDate.getDate()) : (fullDate.getDate());
                var currentDate = twoDigitDate + "/" + twoDigitMonth + "/" + fullDate.getFullYear();
                return currentDate;
            }
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.