我在angularjs中有3个作用域,一个来自一年,一个来自一个月,一个来自一天,来自3个输入框。我想将它们合并为日期,以下代码正确插入日期,但添加了22:00:00,如2019-06-01 22:00:00.000。 如何只插入日期而没有时间
HTML
<div class="col-md-1">
<div class="form-group has-error">
<label class="title_lable">Year:</label>
<input id="code" type="number" class="form-control input-sm"
ng-model="year">
</div>
</div>
<div class="col-md-2">
<div class="form-group has-error">
<label class="title_lable">Latin Description:</label>
<input type="text" class="form-control input-sm"
ng-model="latindesc">
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label class="title_lable">Local Description:</label>
<input type="text" class="form-control input-sm"
ng-model="localdesc">
</div>
</div>
<div class="col-md-1">
<div class="form-group">
<label class="title_lable">Month:</label>
<select ng-show="lang==1" class="form-control input-sm"
ng-model="month"
ng-options="m.mnthid as m.mnthanm for m in months">
</select>
<select ng-show="lang==0" class="form-control input-sm"
ng-model="month"
ng-options="m.mnthid as m.mnthenm for m in months">
</select>
</div>
</div>
<div class="col-md-1">
<div class="form-group">
<label class="title_lable">Day:</label>
<input type="number" class="form-control input-sm" ng-model="day">
</div>
</div>
角度
var dFrom = new Date($scope.month + '/' + ($scope.day + 1) + '/' + $scope.year );
var days = $scope.ndays;
console.log(days);
var todate = new Date($scope.month + '/' + ($scope.day +1) + '/' + $scope.year);
todate = addDays(todate, days - 1);
console.log(todate);
var vacation = {
Sys_Key: $scope.syskey,
Vac_Code: $scope.code,
L_Desc: $scope.latindesc,
A_Desc: $scope.localdesc,
V_Start_Date: $scope.day,
V_Month: $scope.month,
V_Days_Count: $scope.ndays,
V_Year: $scope.year,
From_Date: dFrom,
To_Date: todate
}
function addDays(startDate, numberOfDays) {
var returnDate = new Date(
startDate.getFullYear(),
startDate.getMonth(),
startDate.getDate() + numberOfDays,
startDate.getHours(),
startDate.getMinutes(),
startDate.getSeconds()
);
return returnDate;
}
答案 0 :(得分:1)
添加javascript时,请确保您使用的是数字值而不是字符串。您可以使用parseInt(string)
答案 1 :(得分:1)
代码正确插入了日期,但添加了22:00:00,例如2019-06-01 22:00:00.000。如何只插入日期而没有时间
幕后日期被解析为UTC时间00:00小时。标准的.toString
方法将其显示为本地时间。
var d = new Date("2019-06-01");
console.log(d.toString());
console.log(d.toISOString());
如果您希望将其解析为本地时间,请附加“ T00:00”
var d = new Date("2019-06-01T00:00");
console.log(d.toString());
console.log(d.toISOString());
如果没有时区偏移,则仅日期形式将被解释为UTC时间,而日期时间形式将被解释为本地时间。
有关更多信息,请参见
答案 2 :(得分:0)
您可以这样做
let dateString = "05/05/1997";
let date = new Date(dateString);
console.log(date.toLocaleString('en-US', {
day:"2-digit",
month:"2-digit",
year:"numeric"
}));
请通过以下链接签出
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString