ฉันต้องการทราบวิธีใช้ฟังก์ชัน Date () ใน jQuery เพื่อรับวันที่ปัจจุบันในyyyy/mm/dd
รูปแบบ
ฉันต้องการทราบวิธีใช้ฟังก์ชัน Date () ใน jQuery เพื่อรับวันที่ปัจจุบันในyyyy/mm/dd
รูปแบบ
คำตอบ:
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 นี้เพื่อเปรียบเทียบ
หากคุณมี jQuery UI (จำเป็นสำหรับ datepicker) สิ่งนี้จะทำเคล็ดลับ:
$.datepicker.formatDate('yy/mm/dd', new Date());
jQuery คือ JavaScript ใช้Date
วัตถุJavascript
var d = new Date();
var strDate = d.getFullYear() + "/" + (d.getMonth()+1) + "/" + d.getDate();
getMonth()
ตัวเลขผลตอบแทนระหว่างและ0
11
นี่เป็นข้อผิดพลาดทั่วไปของ JavaScript นอกจากนี้ยังtoString()
ทำงานในลักษณะที่แตกต่างกันกว่าที่คุณอธิบาย (ดูjsfiddle นี้และหน้าเอกสารนี้ ) เพื่อสรุป: ไม่มีวิธีแก้ไขปัญหาใดที่คุณจัดหามาให้อย่างเหมาะสม
toString
แม้ว่าฉันจะสาบานได้ แต่ฉันทดสอบ jsFiddle ของคุณด้วย Chrome และคุณพูดถูก ลบออกจากคำตอบของฉัน ขอบคุณ
การใช้ 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
ใน JavaScript คุณสามารถรับวันที่และเวลาปัจจุบันโดยใช้วัตถุ Date;
var now = new Date();
นี่จะเป็นเวลาของเครื่องไคลเอ็นต์โลคัล
ตัวอย่างสำหรับ jquery LINK
หากคุณใช้ jQuery DatePicker คุณสามารถนำไปใช้กับ textfield เช่นนี้:
$( "#datepicker" ).datepicker({dateFormat:"yy/mm/dd"}).datepicker("setDate",new Date());
เนื่องจากคำถามถูกแท็กเป็นJQuery
:
หากคุณกำลังใช้อยู่JQuery UI
คุณสามารถใช้$.datepicker.formatDate()
:
$.datepicker.formatDate('yy/mm/dd', new Date());
ดูการสาธิตนี้
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;
}
ฟังก์ชั่นที่ใช้งานง่ายและสนุก
ดูนี่สิ วิธีการจดชวเลขจำนวนที่ส่งกลับโดยการแสดงออกที่$.now()
(new Date).getTime()
นี่คือวิธีการด้านบนรับวันปัจจุบันปีหรือเดือน
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)
Moment.jsทำให้มันง่ายมาก:
moment().format("YYYY/MM/DD")
//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
วัตถุนี้ตั้งค่าเป็นศูนย์เมื่อองค์ประกอบมีสัญลักษณ์เดียวเท่านั้น:
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());
});
ฉันรู้ว่าฉันมาสาย แต่นี่คือทั้งหมดที่คุณต้องการ
var date = (new Date()).toISOString().split('T')[0];
toISOString () ใช้ฟังก์ชั่นที่สร้างขึ้นจากจาวาสคริปต์
cd = (new Date()).toISOString().split('T')[0];
console.log(cd);
alert(cd);
คุณสามารถทำสิ่งนี้ได้ด้วย moment.js เช่นกัน รวม moment.js ใน html ของคุณ
<script src="moment.js"></script>
และใช้รหัสด้านล่างในไฟล์สคริปต์เพื่อรับวันที่จัดรูปแบบ
moment(new Date(),"YYYY-MM-DD").utcOffset(0, true).format();
ลองสิ่งนี้ ....
var d = new Date();
alert(d.getFullYear()+'/'+(d.getMonth()+1)+'/'+d.getDate());
getMonth () ส่งคืนเดือน 0 ถึง 11 ดังนั้นเราจึงต้องการเพิ่ม 1 เดือนที่ถูกต้อง
อ้างอิงโดย: http://www.w3schools.com/jsref/jsref_obj_date.asp
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
หน้าปลั๊กอิน jQueryลง ดังนั้นด้วยตนเอง:
function strpad00(s)
{
s = s + '';
if (s.length === 1) s = '0'+s;
return s;
}
var now = new Date();
var currentDate = now.getFullYear()+ "/" + strpad00(now.getMonth()+1) + "/" + strpad00(now.getDate());
console.log(currentDate );
console.log($.datepicker.formatDate('yy/mm/dd', new Date()));
var d = new Date();
var today = d.getFullYear() + '/' + ('0'+(d.getMonth()+1)).slice(-2) + '/' + ('0'+d.getDate()).slice(-2);
สิ่งนี้จะให้สตริงวันที่ปัจจุบันแก่คุณ
var today = new Date().toISOString().split('T')[0];
คุณสามารถทำได้:
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);
คุณสามารถใช้รหัสนี้:
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;
คุณสามารถค้นหาตัวอย่างการทำงานที่นี่
นี่คือสิ่งที่ฉันคิดขึ้นโดยใช้ 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;
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);
ฉันแค่อยากจะแบ่งปันต้นแบบเวลาประทับที่ฉันทำโดยใช้ความคิดของปิแอร์ คะแนนไม่เพียงพอที่จะแสดงความคิดเห็น :(
// 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
การใช้ jQuery-ui datepicker นั้นมีการแปลงวันที่สะดวกในตัวคุณจึงสามารถจัดรูปแบบวันที่:
var my_date_string = $.datepicker.formatDate( "yy-mm-dd", new Date() );
ง่าย
รับรูปแบบวันที่ปัจจุบัน 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);
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);
คุณสามารถเพิ่มวิธีการขยายไปยังจาวาสคริปต์
Date.prototype.today = function () {
return ((this.getDate() < 10) ? "0" : "") + this.getDate() + "/" + (((this.getMonth() + 1) < 10) ? "0" : "") + (this.getMonth() + 1) + "/" + this.getFullYear();
}
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;
}