我是JavaScript新手并尝试计算日期差异。我试图从date
获得一个text-box
,从date
获得另一个date-picker
。但在计算差异时,它给了我错误的答案。
这是我的代码:
$('#ELEdatepicker1').datepicker({
autoclose: true,
startDate: new Date(),
todayHighlight: true,
}).change(function(){
getELEdifference($(this));
});
// var ELEexpDate = document.getElementById('#ELEtextDate').value;
var ELEexpDate = document.getElementById('#ELEtextDate');
function getELEdifference($this)
{
if($this.datepicker("getDate") != null)
{
var ELEcertDiff= ELEexpDate - $this.datepicker("getDate");
var result = Math.round(ELEcertDiff / (1000 * 60 * 60 * 24) * -1);
if(result == 1095 || result == 1825 || result == 3652)
{
document.getElementById("ELEcertDiff").value = result;
} else {
alert('Are you sure ?');
document.getElementById("ELEcertDiff").value = result;
}
}
}
这是我的 jsfiddle: https://jsfiddle.net/2trf5a8y/32/
我很困惑我犯了错误。请帮助我,提前致谢。
答案 0 :(得分:1)
你需要在getElementById时删除#,并获取其值。获得此值后,您需要使用新的日期(..参数)将其转换为日期,然后您可以从datepicker中减去日期。两者之间的差异为毫秒。你可以除以1000秒,或者再换60分钟,依此类推。
public class MyCalendarRecyclerViewAdapter extends RecyclerView.Adapter<MyCalendarRecyclerViewAdapter.Holder> {
private List<CalendarPojo> calendarImagesList;
Context context;
public MyCalendarRecyclerViewAdapter() {
calendarImagesList = new ArrayList<>();
}
@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
View item = LayoutInflater.from(parent.getContext()).inflate(R.layout.calendar_image_item, parent, false);
return new Holder(item);
}
@Override
public void onBindViewHolder(Holder holder, int position) {
CalendarPojo calendarObject = calendarImagesList.get(position);
/* String link = calendarObject.getImage();
// Uri images = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;
Picasso.with(holder.itemView.getContext())
.load(link).into(holder.imageView); */
imageMediaQuery();
}
@Override
public int getItemCount() {
return calendarImagesList.size();
}
public void addCalendarImages(CalendarPojo calendarPojo) {
calendarImagesList.add(calendarPojo);
notifyDataSetChanged();
}
public class Holder extends RecyclerView.ViewHolder {
private ImageView imageView;
public Holder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.select_image_calendar);
}
}
private void imageMediaQuery() {
Context context;
String[] projection = new String[]{
MediaStore.MediaColumns.DATA,
//MediaStore.Images.Media._ID,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
MediaStore.Images.Media.DATE_TAKEN
};
Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String BUCKET_GROUP_BY =
"1) GROUP BY 1,(1";
String BUCKET_ORDER_BY = MediaStore.Images.Media.DATE_TAKEN + " DESC";
Cursor cur = this.getContentResolver().query(images,
projection, // Which columns to return
BUCKET_GROUP_BY, // Which rows to return (all rows)
null, // Selection arguments (none)
BUCKET_ORDER_BY // Ordering
);
if(cur!=null){
Log.i("ListingImages", " query count=" + cur.getCount());
}
if (cur!=null&&cur.moveToFirst()) {
String bucket;
String path;
int bucketColumn = cur.getColumnIndex(
MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
int pathColumn = cur.getColumnIndex(
MediaStore.MediaColumns.DATA);
do {
bucket = cur.getString(bucketColumn);
path = cur.getString(pathColumn);
} while (cur.moveToNext());
cur.close();
}
}
}
答案 1 :(得分:1)
我在您的代码中发现了两个问题。行document.getElementById('#ELEtextDate')
不会给出文本框的值。你必须改变任何一个。
document.getElementById('ELEtextDate').value
或
$('#ELEtextDate').val()
现在第二个问题是你不能直接减去非日期值,意味着你必须将你的值转换为日期格式,然后你可以减去。这条线
var ELEcertDiff= ELEexpDate - $this.datepicker("getDate");
应该改为
var ELEcertDiff= new Date(ELEexpDate) - $this.datepicker("getDate");
我在以下代码段
中实现了相同的功能
$('#ELEdatepicker1').datepicker({
autoclose: true,
startDate: new Date(),
todayHighlight: true,
}).change(function(){
getELEdifference($(this));
});
// var ELEexpDate = document.getElementById('#ELEtextDate').value;
var ELEexpDate = document.getElementById('ELEtextDate').value;
function getELEdifference($this)
{
if($this.datepicker("getDate") != null)
{
var ELEcertDiff= new Date(ELEexpDate) - $this.datepicker("getDate");
var result = Math.round(ELEcertDiff / (1000 * 60 * 60 * 24) * -1);
if(result == 1095 || result == 1825 || result == 3652)
{
document.getElementById("ELEcertDiff").value = result;
} else {
alert('Are you sure ?');
document.getElementById("ELEcertDiff").value = result;
}
}
}
&#13;
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
Date:<input type="text" id="ELEdatepicker1"><br><br>
Diff:<input type="text" id="ELEcertDiff"><br><br>
Days:<input type="text" id="ELEtextDate" value="01/01/2012"><br><br>
&#13;