js中date类型转String类型
<th><em>*</em><label for="lastNewOrgCode">有效起始时间:</label></th>
<td>
<input class="width_c" οnfοcus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss', onpicked:changeEndTime()})"type="text" id="startTime" </td>
<th><em>*</em><label for="lastNewOrgCode">有效结束时间:</label></th>
<td>
<input class="width_c"οnfοcus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',minDate:$('#startTime').val()})"type="text" id="endTime" </td>
要求:在原来时间基础上加上2天
时间设置有两种⽅式:
第⼀种
在系统默认的时间增加2天
function initDateTime() {
var d = new Date();
var vYear = d.getFullYear();
var vMon = d.getMonth()+1;
var vDay = d.getDate();
var hours = d.getHours();
var minutes = d.getMinutes();
var s = d.getSeconds();
var currDay = d.getDate()+2;
var today = vYear + "-" + (vMon<10 ? "0" + vMon : vMon) + "-" + (vDay<10 ? "0"+ vDay : vDay)+" "+(hours<10 ? "0"+ hours : hours)+":"+(minutes<10 ? "0"+ min var current = vYear + "-" + (vMon < 10 ? "0" + vMon : vMon) + "-"
+ (currDay<10 ? "0"+ currDay : currDay) + " "
+ (hours < 10 ? "0" + hours : hours) + ":"
+ (minutes < 10 ? "0" + minutes : minutes) + ":"
+ (s < 10 ? "0" + s : s);
// var current = vYear + "-" + (vMon<10 ? "0" + vMon : vMon) + "-" + (currDay<10 ? "0"+ currDay : currDay)+" 23:59:59";
document.forms[0].startTime.value = today ;
document.forms[0].endTime.value = current;
document.forms[0].newDate.value=today;
}
initDateTime();
第⼆种
在时间控件⾥⾯设置⼀个点击事件
******************js中date转String*************************************** function changeEndTime(){
//起始时间
var startTime = $("#startTime").val();
var str2= dateTimes(startTime,2);
$("#endTime").val(str2)
}
function dateTimes(date, days) {
if (days == undefined || days == '') {
days = 1;
}
var date = new Date(date);
date.Date() + days);
var month = Month() + 1;
var day = Date();
var hh= Hours();
var Minutes();
var Seconds();
FullYear() + '-' + getFormatDate(month) + '-' + getFormatDate(day)+" "+hh+':'+mm+':'+ss; }
// ⽇期⽉份/天的显⽰,如果是1位数,则在前⾯加上'0'
function getFormatDate(arg) {
if (arg == undefined || arg == '') {
return '';
}
var re = arg + '';
if (re.length < 2) {
re = '0' + re;
}
return re;
}
************************针对时间控件的第⼆种思路************************************************
当获取到StartTime时,利⽤时间戳也可以进⾏时间加减
function batchchangeEndTime(){
//起始时间
var batchStartTime = $("#batchStartTime").val();
console.log(batchStartTime);
var timestamp = Date.parse(new Date(batchStartTime));
var timestamp2 = timestamp/1000;
console.log(timestamp2);
var timestamp3 = timestamp2+172800;
//console.log("-------"+timestamp3);
//将时间戳转换为格式⽇期
///var batch = timeFormat(timestamp2);
$("#batchEndTime").val(timeFormat(timestamp3));
}
function timeFormat(timestamp3){
//timestamp是整数,否则要parseInt转换,不会出现少个0的情况
var time = new Date(timestamp3*1000);
var year = FullYear();
var month = Month()+1;
string转date的方法var date = Date();
var hours = Hours();
var minutes = Minutes();
var seconds = Seconds();
return year+'-'+add0(month)+'-'+add0(date)+' '+add0(hours)+':'+add0(minutes)+':'+add0(seconds); }
function add0(m){return m<10?'0'+m:m}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论