I want to add number of days with an existing date fetch from the database.When the timezone is (UTC+ 8:OO)Pacific Time US&CANADA momentjs addition working perfectly,But when i change timezone to (UTC+ 5:3O)Chennai,Kolkatta Momentjs addition not working,It returns the date that we given to add.
var inputtilldate =moment($scope.lasttilldate).add($scope.remdays-0, 'days');
This is the code for addition
Add format to the moment()
var inputtilldate =moment($scope.lasttilldate).add($scope.remdays-0, 'days').format();
Related
When I try to make a post request with React js to a make a reservation the time diminishes by two hours, while in the state it is exactly the time I wanted, meanwhile in the DB it is saved with two hours less. Example I try to save 11 o'clock instead saves 9 o'clock.
This is how format the date and time before passing it to the api call
const booking_date = new Date(year, month, day, hour, minute);
You can use a timestamp to get a more accurate consistent date
const booking_date = new Date(year, month, day, hour, minute).getTime();
Then if you need an actual date string you can convert it back to a date using new Date(). This is likely a timezone issue so a timestamp would mitigate that, along with giving you the extra bonus of being able to send less data in the api call.
Alternatively, if you NEED a string date you can use:
new Date().toUTCString()
which will convert the date to a UTC string that is consistent across the world (it will give you the same value no matter your location) since it uses the standardised UTC timezone.
See more here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
You can use moment.js. If you have a date string then convert it using moment.js before sending the post request or if you do not have any date string, you just need to pick the current date and time means then you can use as like below with the desired format you need. I'd recommend to always use UTC internally, and convert to a timezone only when displaying the date to the user
import moment from "moment";
let date = moment().format('MM-DD-YYYY hh:mm:ss')} // It will return 06-16-2020 08:54:00
I need to use a date range picker in which I need to implement the logic
Start date can be upto 6 months from today well end date will can be up to 6 months from a start date in a single date range selector
Well I played with different packages but I can able to set a limit on any one date.
Suggestions are appreciated
You can use react-calendar with moment, it supports some props two of which are minDate and maxDate.
Render two instances of react-calendar, pass minDate={moment().subtract(6, 'months')} and maxDate={moment()}.
Disable the second instance by default or render it only when user select a date in the first instance of the calendar.
Then pass minDate={dateSelectedInFirstInstance.add(1, 'day')} and maxDate={dateSelectedInFirstInstance.add(6, 'month')}.
I have a property created_at which stores Date and time of users that sign in for. So using Moment.js I want to get the number of days from "latest date" -"created_at".
moment(new Date()).diff(tag.created_at)
You should be able to get the number of days between to dates a and b as follows (using Moment):
a.diff(b, 'days')
I'm currently implementing moment in my React project to format dates of schedules set up in the database for sending notifications. My system has a dashboard displaying all notifications scheduled to go out and their respective dates. The database currently has two columns for recording the scheduled date and time - schedule_date and schedule_time - and currently I display them on the dashboard using the following code:
{moment(file.schedule.schedule_date).format('DD/MM/YYYY')}
{moment(file.schedule.schedule_time, 'HH:mm:ss').format('h:mma')}
schedule_date has the format YYYY-MM-DD and schedule_time has the format HH:MM:SS (pseudo) However, I need now to be able to compare this date with the current date in order to determine whether or not the set schedule for a notification has passed. I used the following code to get today's date and time:
let now = moment();
But I am having difficulty being able to compare this against the actual date / time in the database. I don't want to just compare against the date because if the notification was due to go out at 9.00am and it's now 10.00am it would still assume it hadn't been sent if it was only compared against date.
Is there a way to do this comparison in the front end?
You can set the time to your date's moment object this way :
var date = '2018-09-12T00:00:00Z';
var time = '16:00:00';
var momentDate = moment(date);
var momentTime = moment(time, 'HH:mm:ss');
momentDate = momentDate.set({
hour: momentTime.get('hour'),
minute: momentTime.get('minute'),
second: momentTime.get('second')
});
console.log(momentDate.format());
console.log(momentDate.isAfter(moment.now()));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script>
You can also try to create the iso date format from your date and time strings directly like this :
var fullDate = date + 'T' + time + 'Z';
I am super confused with date formats and need some clarifications. I am trying to pre-populate a form with a date and have set $scope.selectedDate = c.data.Appt.enrolled.start_date;
In my console, c.data.Appt.enrolled.start_date is a string:
However, when I set $scope.selectedDate to that, nothing shows up.
Conversely, if I add new Date in front
(new Date(c.data.Appt.enrolled.start_date))
a date shows up, but it is one day before (April 24, 2018).
In addition to that, when I try to insert the "new Date" version into a function (even though it isn't the correct date), I get a warning in the console saying "Moment construction falls back to js Date. This is discouraged and will be removed in an upcoming major release."
Can someone explain how I should format the dates so I: 1) get the correct date and not one day prior and 2) am able to plug it into a function without getting that warning?
Thanks!
Since the date is a string with no time zone information (just the date) JavaScript Date parser will treat it as universal(UTC, which is in Greenwich Mean Time) time at 00:00 hours. Then it will subtract the offset of your locale's timezone in hours, and will result in the date being a few hours before or after the day you actually want. This is a common point of confusion.
The best way to solve this is to parse the date manually:
function localDate(dateString) {
var d = dateString.split(/\D/);
return new Date(d[0], d[1]-1, d[2]);
}
See this question for more information: Javascript: parse a string to Date as LOCAL time zone
Moment.js gives that warning because it's considered bad practice to rely on the string parsing that new Date() does since it will have different results in different browsers (IE\Firefox\Etc.). It's more cross-browser friendly to build the date using this form: new Date(year, month, day). (Note that the month starts at zero, not 1)