Using moment.js trying to create an event in native IOS calendar - javascript

I have used the "datetime-local" input to get a start date and an end date from the user to put into the native calendar. This is my code to get and format the "datetime-local":
var s = $("#startDate").val();
var startDate = moment(s).toDate();
var e = $("#endDate").val();
var endDate = moment(s).toDate();
This code takes the correct date, but it makes the time set to an all day event. For example, if I put in 1:00 o'clock on 7/21/2014 as the start date and then 1:00 o'clock on 7/22/2014 it will create an all day event on 7/21/2014.
Here is a JSFiddle

You appear to have a typo. Pass e to build the endDate, not s.
var s = $("#startDate").val();
var startDate = moment(s).toDate();
var e = $("#endDate").val();
var endDate = moment(e).toDate(); // <==== this line had used the wrong variable
Or, you could instead just call these inline to avoid confusion:
var startDate = moment($("#startDate").val()).toDate();
var endDate = moment($("#endDate").val()).toDate();
As to why this created an "all day event" - I can't be absolutely certain since I'm not familiar with the particular calendar API you're using. However, it's possible that since you were sending the same start and end time, that it interpreted that as an all day event. I believe fixing the typo will solve the issue.

Related

How to Compare Date time in moment js

Hi I need to validate two date time moment instances,
Using this Package to get time we can select hour min sec in this.
I need validate if selected Date time is after or before like you see in code .I'm having confusion will it validate date with time ?
How to compare selected date time hour min sec in isAfter/isBefore
function DateValidate(selectedDate,type){ //selected date in moment instance
let isValid=false
const {startTime,endTime}=dateTime // from state
if(type==='start'&& endTime){
isValid=selectedDate.isAfter(endTime);
}else if(type==='end'&& startTime){
isValid=selectedDate.isBefore(startTime);
}
return isValid
}
should i need to format selected date needed format -(dd/mm/yyyy hh:mm:ss) and then apply isBefore/isafter ?
Please help...
Yes it will validate date along with the time. Here is the code which shows the 1 second difference between two dates.
const out = document.getElementById('output');
const today = moment();
var afterToday;
setTimeout(()=>{
afterToday = moment();
}, 1000)
setTimeout(()=>{
//const diff = afterToday.isAfter(today);
const diff = today.isAfter(afterToday);
out.innerText = diff;
}, 2000)
To see it in action take a look here.
It looks like, in your example, the selectedDate is not a valid moment object. selectedDate needs to be a valid moment object in order to successfully apply the isAfter() and isBefore() methods, and startTime and endTime must also be in the correct format. See the examples in the docs: https://momentjs.com/docs/#/query/is-after/.
If you know the format ahead of time, you can pass that as the second argument into moment(). See the relevant part of the docs: momentjs.com/docs/#/parsing/string-format. In your example, something like moment("10/09/1997 02:31:01", "dd/mm/yyyy hh:mm:ss")

adding Date and Time in javascript to get in ISOFormat

I got the date in ISOFormats and time in 12hrs format. i need to combine togeather and get the output in ISOFormat using javascript. Im doing this in reactJs so using external libraries is fine
date = "2019-02-19T00:00:00.000Z"
startTime = "04.42PM"
outputDateTime = "2019-02-19T11:12:37.680Z"
Have a look at momentjs parse function.
Use it to convert the dates to moment objects and directly add them using the add function.
Example here
If you go pure vanilla I think this is fairly simple (AFAIK you only need hours and minutes and the zone is always fixed, if not, upgrade).
var yourDate = "2019-02-19T00:00:00.000Z";
var yourTime = "04.42PM"
var dat = yourDate.split("T")[0];
var minutes = yourTime.split(".")[1].slice(0,2);
var isPm = yourTime.split(".")[1].slice(2) === "PM";
var hours = isPm ? parseInt(yourTime.split(".")[0]) + 12 : yourTime.split(".")[0];
var date = new Date(dat+ "T" +hours+":"+minutes+":00Z");
Basically, I decomposed the input strings into interesting parts, compensated for PM if needed and put them back together :)

How to convert html5 input time to a timestamp in JavaScript?

Let's say I have 2:12 PM as my time from an input, and I want to convert it to a timestamp combined with the current date.
I can get the current timestamp using Date.now() but my problem is I want the time to be based on the input.
If moment can be used, then better. Any help would be much appreciated.
You could pass in the time format to moment's constructor, along with the input's value as string to parse it into a moment object. Like this:
console.log(moment('2:12 PM',"hh:mm a").format('lll'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
You should be able to use momentJS custom parsing:
moment("2:12 PM", "H:mm A");
Using simple JavaScript
Take date
var d = new Date();
Split it
var l = d.toString().split(":");
Slice it
var f =l[0].slice(0,-2);
Get your time variable
var ty="11:22:00";
Create Date
var j = new Date(f + ty);
Done, It's in j
One line solution:
var d = new Date();
var ty = "11:22:00";
var newDate = new Date(d.toString().split(":")[0].slice(0,-2) + ty);
It's the full date, you can use and change it as you like.

Get 4 week startdate and enddate

I want to get start and enddates for upcoming 4 weeks(only weekdays).
Suppose today's date is 2015-12-01 then the result should be as below
Week0 will have StartDate = 2015-11-30 and EndDate = 2015-12-04
Week1 will have StartDate = 2015-12-07 and EndDate = 2015-12-11
Week2 will have StartDate = 2015-12-14 and EndDate = 2015-12-18
Week3 will have StartDate = 2015-12-21 and EndDate = 2015-12-25
Here date of Week0 should be calculated from current date.
Try the moment library. It's pretty easy to use, so you should be able to figure out quickly, how to do this.
var date = moment(yourdate);
// iterate by date.add(1, "week")
var start = date.clone().startOf('week');
var end = date.clone().endOf('week');
//use .format('YYYY-MM-DD'); to print out
Here is how you would use the Moment.js library (as mentioned in the comments) to achieve the output you desire. It's quite easy by using the built in functions (to see the result, hit F12 on your keyboard or open the console some other way)
var weeks = 4;
for (var i = 0 ; i < weeks ; i++) {
var start = moment().startOf('isoweek').add(i, 'weeks');
var end = start.clone().add(4, 'days');
console.log("Week%d will have StartDate = %s and EndDate = %s", i, start.format('YYYY-MM-DD'), end.format('YYYY-MM-DD'));
}
<script src="http://momentjs.com/downloads/moment.min.js"></script>
Couple simple built in functions at work here, namely:
moment, which is an instance of the moment class - essentially a datetime string.
startOf, pretty self explanatory, finds the exact datetime of when (in this case) the start of the week was
add, which adds a certain amount of x i.e. days, weeks, months etc. to the moment instance
clone, a necessary step which clones the original moment to prevent it from being modified by the end variable.
and format, pretty obvious, formats the moment based on the string given as its argument.
Take a look at the Moment.js docs and have a little decipher of the code; it will help you understand Moment.js as a library much better. Hope this helps.

TimeZone in different browsers

i know it was asked a hundred of times how to change timezone in javaScript, My problem is that time zone is displayed different in FireFox and IE, 4 hours difference(my difference with UTC)
Here is my code:
var startDate = new Date(rec[ns.$startDate]);
var utc = startDate.getTime() + startDate.getTimezoneOffset()*60*1000;
return new Date(utc);
maybe there is another way, for example get timezone from a string...
try to use another variable to getTimeOffset...
var startDate = new Date(rec[ns.$startDate]);
var d = new Date();
var utc = startDate.getTime() + d.getTimezoneOffset()*60*1000;
return new Date(utc);
If I understand correctly, you need the current time zone of the user in the form of string.
Try this code:
Date().match(/GMT.\d+/).toString()

Categories

Resources