How can decrement the current day or increment in Angular 7 using this property "Date.now()" that is in Documentation ?
Component
today: number = Date.now()
HTML
<td>{{ today | date: 'dd/MM/yyyy '}}</td>
let date= new Date();
let diff = 1; //1 to increment and -1 to decrement
date.setDate(today.getDate() + diff);
100 working solution
today=new Date();
constructor(
) {
console.log('11... today date',this.today);
var tom=this.today.setDate(this.today.getDate()+3);
var tomformatted=formatDate(tom, 'yyyy-MM-dd HH:MM:ss',
'en-US');
console.log('11... tommorow formatted date',tomformatted);
}
Console Logs
Related
I know you can get the current date and time using the Now() function, but how would you get the date of yesterday?
You can use the Yesterday or IncDay(Now,-1) function from System.DateUtils as follow:
uses System.DateUtils;
begin
// Example 1:
ShowMessage('Yesterday = ' + DateToStr(Yesterday)); // Date of yesterday
ShowMessage('Today = ' + DateToStr(Date)); // Date of Today
ShowMessage('Tomorrow = ' + DateToStr(tomorrow)); // Date of tomorrow
// Example 2:
ShowMessage('Yesterday = ' + DateToStr(IncDay(Now,-1))); // Date of yesterday
ShowMessage('Today = ' + DateToStr(Now)); // Date of Today
ShowMessage('Tomorrow = ' + DateToStr(IncDay(Now,1))); // Date of tomorrow
end;
These functions return a TDateTime data type. The time component is set to zero.
Why not just use Date - 1 ?
Since one day is 1.0 in TDateTime encoding, substracting 1 is enough.
I have a MVC Web Application Im trying to Ajax call an action method to retrieve some datetime from the database, the problem is the value comes as "/Date(386028000000)/"
its a DateOfBirth actually which I m using a java script function to calculate the age:
function (DOB) {
var birthday = +new Date(DOB);
return ~~((Date.now() - birthday) / (31557600000));
}
Anyway i can fix the Date Format and get only the date in a proper format or change the Java-Script method to accept the current format of the date value ?
I got it
var FixedDate = new Date();
FixedDate .setTime(DOB.replace("/Date(", "").replace(")/", ""));
return ~~((Date.now() - FixedDate) / (31557600000));
Click here to check the Demo
Sample Javascript/JQuery
var = MyDate_String_Value = "/Date(386028000000)/"
var value = new Date
(
parseInt(MyDate_String_Value.replace(/(^.*\()|([+-].*$)/g, ''))
);
var dat = value.getMonth() +
1 +
"/" +
value.getDate() +
"/" +
value.getFullYear();
Result - "3/27/1982"
I don't think I'm hallucinating as I've tried it a dozen times. Here's my code:
$('#teo_prep_due').change(function() {
var ber = $('#ber_rcvd');
var tpd = $('#teo_prep_due');
var brDate = ber.datepicker('getDate');
var tpDate = tpd.datepicker('getDate');
var s1Date = new Date();
var s2Date = new Date();
var sdDate = new Date();
s1Date.setDate(brDate.getDate() + 5);
console.log(s1Date);
s2Date.setDate(tpDate.getDate() - 3);
console.log(s2Date);
if (s1Date < s2Date) {
sdDate.setDate(s1Date.getDate());
} else {
sdDate.setDate(s2Date.getDate());
}
$('#survey_due').datepicker('setDate', sdDate);
});
On my date form, I've entered February 28, 2013 for ber_rcvd and March 14, 2013 for teo_prep_due. Following the code, my result should be March 5, 2013. However, s2Date is resulting in February 11, 2013, as if a full month and 3 days are being subtracted instead of just 3 days. Has anyone else run into this?
Using: jquery-1.9.1.min.js, jquery-migrate-1.1.1.js and jquery-ui-1.10.1.min.js.
http://jsfiddle.net/devlshone/veP7b/
The problem is that .setDate() does not set the date, it sets the day of the month. It takes an integer as an argument. When you created s2Date it defaulted to today, which is in February. When you add tpDate.getDate()// equals 14 with -3, you get 11, and therefore February 11th.
I am trying the new Google Dart language and I don't know how to get the last day of the current month?
This gives me current date:
var now = new DateTime.now();
Providing a day value of zero for the next month gives you the previous month's last day
var date = new DateTime(2013,3,0);
print(date.day); // 28 for February
If you want to get the last date of the current month, you need to refer to the 0th day of the next month
Try this in an easy way:
DateTime now = DateTime.now();
int lastday = DateTime(now.year, now.month + 1, 0).day;
Here's one way to find it:
var now = new DateTime.now();
// Find the last day of the month.
var beginningNextMonth = (now.month < 12) ? new DateTime(now.year, now.month + 1, 1) : new DateTime(now.year + 1, 1, 1);
var lastDay = beginningNextMonth.subtract(new Duration(days: 1)).day;
print(lastDay); // 28 for February
I have the current date, so I construct the first day of the next month, and then subtract one day out of it. I'm also taking the change of the year into account.
Update: Here's a little bit shorter code for the same thing, but inspired by Chris's zero-trick:
var now = new DateTime.now();
// Find the last day of the month.
var lastDayDateTime = (now.month < 12) ? new DateTime(now.year, now.month + 1, 0) : new DateTime(now.year + 1, 1, 0);
print(lastDayDateTime.day); // 28 for February
It has the additional check/code, in case you want to do this programmatically (e.g. you have a specific month as an integer).
Here's an extension that might help. (Ref. from Kai's and Chris's answers.)
extension DateTimeExtension on DateTime {
DateTime get firstDayOfWeek => subtract(Duration(days: weekday - 1));
DateTime get lastDayOfWeek =>
add(Duration(days: DateTime.daysPerWeek - weekday));
DateTime get lastDayOfMonth =>
month < 12 ? DateTime(year, month + 1, 0) : DateTime(year + 1, 1, 0);
}
Another way is using Jiffy. It has the endOf method that makes easy to get the last moment of several units, in this case the month:
Jiffy().endOf(Units.MONTH);
While all this answers are correct & do give you the last DAY of the month :
like so : year-month-lastDay 00:00:00.000 -> beginning of the last day of the month
You might need the last bound / DateTime of the month :
like so : year-month-lastDay 23:59:59.999 -> end of the last day of the month
Here is how to get the two bounds within a month with the help of an extension on DateTime :
extension MonthsBounds on DateTime {
DateTime get lastMillisecondOfMonth =>
month < 12 ? DateTime(year, month + 1, 1, 00, 00, 00, -1) : DateTime(year + 1, 1, 1, 00, 00, 00, -1);
DateTime get firstMillisecondOfMonth => DateTime(year, month, 1);
}
print('${DateTime(2022, 12, 14).lastMillisecondOfMonth}'); // 2022-12-31 23:59:59.999
DateTime first = DateTime(month.value.year, month.value.month, 1);
DateTime end = Jiffy(first).add(months: 1, days: -1).dateTime;
I'm using jQuery UI's DatePicker to select a date of birth for consumers on my website. It is currently showing with the month and year via drop downs When, for example, I restrict the years to -18y, via maxDate, the month drop down does not show all of the months when the year 1993 is selected. It only shows the months up until the maximum months.
In usability testing, we've found that our demographic tends to click the month of their birth, then the year, then the day.
Is there a way to show all of the months, even if the dates within that month are not selectable?
Here is the code used to show the DatePicker:
$('.DobWidget').datepicker({
showOn: 'both',
buttonImage: '/media/img/admin/icon_calendar.gif',
buttonImageOnly: true,
dateFormat: js_date_format, // mm/dd/yyyy
constrainInput: false, // Handled server-side for type-in.
changeMonth: true,
changeYear: true,
maxDate: '-18y',
minDate: '-110y',
showButtonPanel: true,
onChangeMonthYear: function(year, month, inst) {
if (inst.currentDay != 0) {
$(this).datepicker("setDate", new Date(year, month - 1, inst.currentDay));
}
}
});
I am currently using jQueryUI v.1.8.16, and jQuery v.1.6.3, both provided by the Google AJAX API's.
Fix for jquery ui 1.8.17
I also had this problem I got in to work by changing some things in the jquery code. Now I see all months and I can select them but dates will still be unselectable as you want.
In _generateHTML function in the if statement you add maxDraw.setMonth('11'):
if (maxDate) {
maxDraw = (minDate && maxDraw < minDate ? minDate : maxDraw);
maxDraw.setMonth('11');
while (this._daylightSavingAdjust(new Date(drawYear, drawMonth, 1)) > maxDraw) {
...
}
}
In _generateMonthYearHeader function in the for loop of the else statement you change:
for (var month = 0; month < 12; month++) {
if ((!inMinYear || month >= minDate.getMonth()) &&
(!inMaxYear || month <= maxDate.getMonth()))
monthHtml += '<option value="' + month + '"' +
(month == drawMonth ? ' selected="selected"' : '') +
'>' + monthNamesShort[month] + '</option>';
}
becomes
for (var month = 0; month < 12; month++) {
if ((!inMinYear || month >= minDate.getMonth()) &&
(!inMaxYear || month <= 11))
monthHtml += '<option value="' + month + '"' +
(month == drawMonth ? ' selected="selected"' : '') +
'>' + monthNamesShort[month] + '</option>';
}
In _restrictMinMax you add maxDate.setMonth('11'):
...
var maxDate = this._getMinMaxDate(inst, 'max');
maxDate.setMonth('11');
var newDate = (minDate && date < minDate ? minDate : date);
...
In _isInRange you add maxDate.setMonth('11'):
var minDate = this._getMinMaxDate(inst, 'min');
var maxDate = this._getMinMaxDate(inst, 'max');
maxDate.setMonth('11');
...