Reputation: 211
I have 3 scopes in angularjs, one for year, one for month and one for day coming from 3 input boxes. I want to combine them as date, the following code insert the date correctly but with 22:00:00 added to it like 2019-06-01 22:00:00.000. How to insert the date only without time
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>
Angular
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;
}
Upvotes: 2
Views: 878
Reputation: 48948
the code insert the date correctly but with 22:00:00 added to it like 2019-06-01 22:00:00.000. How to insert the date only without time
Under-the-hood the date is parsed to 00:00 hours UTC time. The standard .toString
method displays it as local time.
var d = new Date("2019-06-01");
console.log(d.toString());
console.log(d.toISOString());
If you want it parsed to local time append "T00:00"
var d = new Date("2019-06-01T00:00");
console.log(d.toString());
console.log(d.toISOString());
When the time zone offset is absent, date-only forms are interpreted as a UTC time and date-time forms are interpreted as local time.
For more information, see
Upvotes: 1
Reputation: 402
You can do it as,
let dateString = "05/05/1997";
let date = new Date(dateString);
console.log(date.toLocaleString('en-US', {
day:"2-digit",
month:"2-digit",
year:"numeric"
}));
Please checkout following link,
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
Upvotes: 0
Reputation: 330
When adding in javascript, be sure you are using numerical values and not strings. You can convert strings to integers with parseInt(string)
Upvotes: 1