คำนวณจำนวนวันระหว่างวันที่สองวันด้วย Momentsjs

ฉันมีวันที่สองวันที่เก็บไว้ใน var (สร้างโดยตัวเลือกวันที่) หากต้องการจัดรูปแบบเหล่านี้ ฉันใช้ moments.js

ฉันต้องการคำนวณจำนวนวันระหว่างสองฟิลด์นี้และเก็บตัวเลขนี้ไว้ใน var days นี่คือรหัสของฉันจนถึงตอนนี้:

// When someone clicks my submit button, I create an url stored in the var url.
$(".book_submit").on('click', function(e){

  // I get the datepicker and input fields data
  var from = $('.from').val();
  var to = $('.to').val();

  // I can format the "from" var the way I want
  from  = moment(from, "DD.MM.YYYY");
  from = moment(from).format("YYYY-MM-DD");

  // I can format the "to" var the way I want
  to = moment(to, "DD.MM.YYYY");
  to = moment(to).format("YYYY-MM-DD");

  // I want to store the number between the two dates in the var "days", to place it inside my link below. I have found ".diff(to, 'days') in the moments documentation.

  //days = from.diff(to, 'days');   // =1

  var url = "https://mycustomlink&days= " + days + "";

});

ฉันไม่สามารถให้ from.diff(to, 'days') ทำงานและเก็บไว้ในตัวแปรได้ ฉันได้ลองตั้งค่าดังนี้:

var days = from.diff(to, 'days');

ฉันจะเก็บความแตกต่างระหว่างวันนี้ไว้ใน var days ของฉันได้อย่างไร ฉันขอขอบคุณข้อเสนอแนะใด ๆ


person Marian Rick    schedule 27.01.2016    source แหล่งที่มา


คำตอบ (2)


นอกเหนือจาก .diff() คุณยังสามารถใช้ ช่วงเวลา durations: http://momentjs.com/docs/#/durations/

แก้ไข: ไม่รู้ว่าตอนนั้นฉันคิดอะไรอยู่ ขอบคุณ @aedm สำหรับการชี้ให้เห็น ไม่จำเป็นต้องมีระยะเวลาในที่นี้ ซึ่งมีไว้สำหรับสร้างช่วงเวลา

.diff ทำงานตามที่ตั้งใจ เพียงแต่คุณต้องตรวจสอบให้แน่ใจว่าคุณใช้รูปแบบที่ถูกต้อง เพื่อแยกวันที่

ตัวอย่างซอ: https://jsfiddle.net/abhitalks/md4jte5d/

ตัวอย่างข้อมูล:

$("#btn").on('click', function(e) {
	var fromDate = $('#fromDate').val(), 
  		toDate = $('#toDate').val(), 
		from, to, druation;
  
	from = moment(fromDate, 'YYYY-MM-DD'); // format in which you have the date
	to = moment(toDate, 'YYYY-MM-DD');     // format in which you have the date
  
	/* using diff */
	duration = to.diff(from, 'days')     
	
	/* show the result */
	$('#result').text(duration + ' days');
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.1/moment.min.js"></script>
From: <input id="fromDate" type='date' />
To: <input id="toDate" type='date' />&nbsp;&nbsp;
<button id="btn">Submit</button><hr />
<p id="result"></p>


ดังที่ฉันเห็นในโค้ดของคุณ คุณมีความคิดเห็นดังนี้:

// I can format the "from" var the way I want

ไม่ คุณไม่สามารถจัดรูปแบบวันที่ตามที่คุณต้องการได้ คุณต้องใช้รูปแบบวันที่ที่คุณกำลังแยกวิเคราะห์ ในตัวอย่างของฉัน เนื่องจากฉันใช้วันที่ HTML5 วันที่ส่งคืนจะอยู่ในรูปแบบ yyyy-mm-dd ดังนั้นจึงใช้รูปแบบนั้น ในกรณีของคุณ ให้พิจารณาว่ากล่องข้อความของคุณแสดงวันที่ในรูปแบบใด และใช้รูปแบบนั้น

คุณยังอาจละเว้นรูปแบบทั้งหมดได้ หากคุณแน่ใจว่าวันที่ที่ส่งคืนอยู่ในรูปแบบมาตรฐาน

person Abhitalks    schedule 27.01.2016
comment
คำตอบนี้ผิด ลองเล่นซอของคุณในวันที่ 10/01/2000 และ 11/02/2000 - person aedm; 20.06.2016
comment
เพื่อให้ชัดเจน duration.days() ผิด เนื่องจากจะทำให้คุณเห็นความแตกต่างเพียงบางส่วนเท่านั้น อย่างไรก็ตาม to.diff(from, 'days') ทำงานตามที่ตั้งใจไว้ - person aedm; 23.06.2016
comment
ขอบคุณ @aedm. แก้ไขคำตอบแล้ว. ตอนนั้นคิดอะไรไม่ออก :/ คงตื่นดึกและปะปนกับที่อื่น!! ขอบคุณอีกครั้ง มันคงจะไม่มีใครสังเกตเห็นถ้าไม่ใช่คุณ - person Abhitalks; 28.06.2016
comment
ขอบคุณสำหรับการแก้ไข :) - person aedm; 28.06.2016

หากต้องการทราบความแตกต่างในหน่วยการวัดอื่น ให้ส่งการวัดนั้นเป็นอาร์กิวเมนต์ที่สอง

var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days') // 1

อ้างอิงสิ่งนี้เพื่อดูเพิ่มเติม

person WP Learner    schedule 27.01.2016
comment
ขอบคุณสำหรับข้อเสนอแนะของคุณ! ฉันพบสิ่งนี้ในเอกสารเช่นกัน แต่บอกตามตรงว่าฉันไม่รู้วิธีแทนที่วันที่ที่แสดงในตัวอย่างของคุณด้วย vars ของฉัน คุณช่วยฉันด้วยได้ไหม? - person Marian Rick; 27.01.2016
comment
ตัวแปรแรกคือปี ตัวที่สองคือเดือน และตัวที่สามคือวันที่ ดังนั้น คุณจะต้องผ่านค่าสองค่าโดยสัมพันธ์กัน - person WP Learner; 27.01.2016