MomentJS - displaying now/ tomorrow instead of today/ tomorrow's date - javascript

I'm using a component from AntUI library that renders the date picker.
What I want to do is to display Now and Tomorrow instead of today's/ tomorrow's dates.
Is there a specific momentJS format (I couldn't find any, so probably not) or a way to configure momentJS library that it will automatically display those values instead of those dates?

You could check if the date is between moment().startOf('day'); and moment().endOf('day'); and display Now or Tomorrow depending on the result
moment().isBetween(moment().startOf('day'), moment().endOf('day'));
would be Now (true) and
moment().add(1, 'day').isBetween(moment().startOf('day'), moment().endOf('day'));
and this would be Tomorrow (false)

Try this:
var today = moment().endOf('day')
var tomorrow = moment().add(1, 'day').endOf('day')
var date = new Date();
//Check Today's date
if (date < today)
alert('today')
if (date < tomorrow)
alert('tomorrow')
UPDATE
You can use this as well but it will show current time as well:
moment().calendar(); // Today at 10:37 AM
moment().add(1, 'days').calendar(); // Tomorrow at 10:37 AM

Related

How to find the dates of coming saturday and sunday from today's date in typescript?

I have set today's date in code as follows,
public fromDate: Date = new Date();
How can i find the dates of coming saturday and sunday, mean this weekend dates from the above?
i can try by adding number of days from the current day. but is there a better solution?
If you don't mind using an external library, you can use moment's day function.
import * as moment from 'moment';
const fromDate = moment();
const nextSunday = fromDate.day(7);
console.log(nextSunday.calendar());

Can not compare dates properly using Angular.js/Javascript

I need one help.I am unable to compare the selected date with today's date using Angular.js or JavaScript.I am explaining my code below.
var today=new Date();
console.log('2 dates',today,$scope.date);
From the above console i am getting the output like below.
2 dates Fri Jun 03 2016 18:29:16 GMT+0530 (India Standard Time) 03-06-2016
if(today > new Date($scope.date.split("-").reverse().join(","))){
alert('Please select future delivery date');
}
Here i need suppose my selected date is 03-06-2016 and today's date is 03-06-2016 the alert prompt should not come.But in my case its not happening like this.This alert message is coming also if selected date is 03-06-2016. Here i need the alert message should come if selected date is more than today's date only.Please help me.
Months are zero-indexed when constructing Dates:
console.log(new Date(2016,06,03));
"2016-07-03T04:00:00.000Z" // you expected June, but got July.
Note also that that is set to midnight: any new Date() run during the same day will be greater than that value.
String manipulation of dates is risky and error-prone, which is why everyone's knee-jerking momentjs at you (it is in fact a very good library and worth using if you're doing much date manipulation).
At the least, you may want to consider using a less ambiguous date format in $scope.date, but if you're stuck with that, in this case, you need to subtract 1 from the month when constructing a Date from it:
// this sample code will only work correctly on June 3 2016 :)
var $scope = {
date: "03-06-2016", // I am assuming DD-MM-YYYY format here
tomorrow: "04-06-2016"
};
var today = new Date();
// test against today:
var dArr = $scope.date.split('-');
var testDate = new Date(dArr[2],dArr[1]-1,dArr[0]);
if (today > testDate) {
console.log("today is greater than testDate.");
/* Note that anytime during today will be greater than the zero
o'clock constructed from $scope.date. If what you really want
is to alert only on the following day, then add 1 to the day
of $scope.date when converting it to a Date(). */
}
// now test against tomorrow:
dArr = $scope.tomorrow.split('-');
testDate = new Date(dArr[2],dArr[1]-1,dArr[0]);
if (today < testDate) {
console.log("today is less than (tomorrow's) testDate.");
}
Don't compare JavaScript dates using native < or > operators; install and use moment.js - it will handle all the difficulties for you:
if(moment(new Date()).isAfter(moment($scope.date, "MM-DD-YYYY"), 'day')) { ...
Convert to timestamp to compare it:
$scope.date = '2016-06-01';
var currentDate = new Date().valueOf();
var date = new Date($scope.date).valueOf();
if (currentDate > date) {
// something
}
It you use multi timezone, please convert both to a timezone.

JS: Convert Today's Date to ISOString() with Fixed Time

I'm trying to convert today's date to an ISO standard string but with the fixed time of T00:00:00.000Z.
I can get as far as returning a ISO string of today's date and time:
var isoDate = new Date().toISOString();
// returns "2015-10-27T22:36:19.704Z"
But I wanted to know if it's possible to have a fixed time, so it should return:
"2015-10-27T00:00:00.000Z"
Is this possible?
Any help is appreciated. Thanks in advance!
To get the current UTC date at midnight:
var d = new Date();
d.setUTCHours(0);
d.setUTCMinutes(0);
d.setUTCSeconds(0);
d.setUTCMilliseconds(0);
var output = d.toISOString();
To get the current local date, with the time portion set to UTC midnight:
var d = new Date();
var ts = Date.UTC(d.getFullYear(), d.getMonth(), d.getDate());
var output = new Date(ts).toISOString();
As for which to use, think through your requirements very carefully, The current UTC date and the local date may indeed be two different days.
For example, when it's midnight (00:00) October 27th in UTC, it's 8:00 PM on October 26th in New York.
Also, consider using moment.js, which makes operations like either of these much easier with the startOf('day') and .utc() functions.

populate textbox with formatted date

I have two datepicker fields (telerik, not jquery UI) and also a radio button list containing buttons for week, month, year, etc.
The user can either select a date range using the two datepickers, or alternatively, they can click one of the radio buttons and the fields should populate based on their choice.
So if the user selects week, the end date should be today and the start date should be 7 days ago.
What I have at the moment is this:
$(function () {
$("#dateRange_week").click(function () {
var now = new Date();
var startDate = now;
$("#StartDate").val(startDate);
$("#EndDate").val(now);
});
});
currently the jQuery inserted dates are strings formatted as follows
Tue Oct 02 2012 12:08:01 GMT-0400 (Eastern Daylight Time)
How do I calculate startDate as now - 7 days?
How can I format the dates as mm/dd/yyyy?
EDIT___
The fix:
Using Date.js as per the accepted answer below, the jQuery becomes
$("#dateRange_week").click(function () {
var startDate = (7).days().ago();
var start = startDate.toString("M/d/yyyy");
var endDate = new Date();
var end = endDate.toString("M/d/yyyy");
$("#StartDate").val(start);
$("#EndDate").val(end);
});
It's a pity, but JS doesn't have good datetime manipulation abilities.
Good news - you can use some plugin for that.
For example:
Moment - very good plugin from SO user timrwood
date.js - another good plugin for date and time manipulation
They both (and many others) have functions for adding/subtracting days and date formatting. And many other useful options.
Here is a good start for formatting the jQuery date jQuery date formatting
And this might help for subtracting days
Adding/Subtracting days from a date doesn't change the year/month correctly
Try something like:
DateTime dtNow = DateTime.Now;
DateTime dtBegin = DateTime.MinValue;
DateTime dtEnd = DateTime.MinValue;
dtBegin = dtNow.AddDays(1 - Convert.ToDouble(dtNow.DayOfWeek));
dtEnd = dtNow.AddDays(7 - Convert.ToDouble(dtNow.DayOfWeek));
To formate mm/DD/yyyy use .ToShortDateString()

JavaScript For Checking Date

function checkDate(sender, args) {
var toDate = new Date();
toDate.setMinutes(0);
toDate.setSeconds(0);
toDate.setHours(0);
toDate.setMilliseconds(0);
if (sender._selectedDate > toDate) {
alert("You can not select forthcoming days!");
sender._selectedDate = toDate;
sender._textbox.set_Value(sender._selectedDate.format(sender._format))
}
I used this javascript to validate the date of the textbox. Its should only accept the all the past date and todays date. Future dates are not accepted. It works perfectly for past and future date. But when i select the todays date it do not accept. The logic is correct because selected date should be greater than todays date as i have given. Can any one give a suggestion on this.
You're checking against the start of the day (ie. 0:00). So unless they go on at exactly midnight selectedDate will be > toDate. You want to set the time fields to 23:59:59 to allow any time during the current day.

Categories