comparing dates with javascript asp.net mvc - javascript

i'm beginnig with javascript and my case seems really complicated for me !
i'm trying to compare today's date to "DateFinReele" date
my "DateFinReele" date hs to be smaller than today's date
i have another control on the "DateDebut" , "DateFinPrevue" and "DateFinReele"
the "DateFinPrevue" and "DateFinReele" have to be smaller than the "DateDebut"
when i remove my function checkDateInpuWithTodays() the other control works very well but when i add it nothing is working (even the other control !!!)
script >
$(document).ready(function() {
function DateDebutChange() {
var DateDebutDate = DateDebut.value(),
DateFinPrevueDate = DateFinPrevue.value(),
DateFinReeleDate = DateFinReele.value();
if (DateDebutDate) {
DateDebutDate = new Date(DateDebutDate);
DateDebutDate.setDate(DateDebutDate.getDate());
DateFinPrevue.min(DateDebutDate);
DateFinReele.min(DateDebutDate);
} else if (DateFinPrevueDate) {
DateDebut.max(new Date(DateFinPrevueDate)); <
} else if (DateFinReeleDate) {
DateDebut.max(new Date(DateFinReeleDate));
} else {
DateFinPrevueDate = new Date();
DateFinReeleDate = new Date();
DateDebut.max(DateFinPrevueDate);
DateFinPrevue.min(DateFinPrevueDate);
}
}
function DateFinReeleChange() {
var DateFinReeleDate = DateFinReele.value(),
DateDebutDate = DateDebut.value(),
DateFinPrevueDate = DateFinPrevue.value();
if (DateFinReeleDate) {
DateFinReeleDate = new Date(DateFinReeleDate),
DateFinPrevueDate = DateFinPrevue.value(),
DateDebutDate = DateDebut.value();
} else if (DateDebutDate) {
DateFinPrevue.min(new Date(DateDebutDate)),
DateFinReele.min(new Date(DateDebutDate));
} else {
DateFinPrevueDate = new Date();
DateDebut.max(DateFinPrevueDate)
DateDebut.max(DateFinReeleDate)
DateFinPrevue.min(DateFinPrevueDate);
DateFinReele.min(DateFinReeleDate)
}
}
function DateFinPrevueChange() {
var DateFinPrevueDate = DateFinPrevue.value(),
DateDebutDate = DateDebut.value();
if (DateFinPrevueDate) {
DateFinPrevueDate = new Date(DateFinPrevueDate);
DateFinPrevueDate.setDate(DateFinPrevueDate.getDate());
DateDebut.max(DateFinPrevueDate);
} else if (DateDebutDate) {
DateFinPrevue.min(new Date(DateDebutDate));
} else {
DateFinPrevueDate = new Date();
DateDebut.max(DateFinPrevueDate)
DateFinPrevue.min(DateFinPrevueDate);
}
}
function checkDateInpuWithTodays() {
var x = new Date();
var DateFinReeleDate = DateFinReele.value();
if (DateFinReeleDate.value > x.valueOf()) {
alert("La date de fin du projet doit être inférieure à celle d'aujourd'hui !")
DateFinReele.value = null;
DateFinReele.ready;
}
}
var DateDebut = $("#DateDebut").kendoDatePicker({
change: DateDebutChange
}).data("kendoDatePicker");
var DateFinPrevue = $("#DateFinPrevue").kendoDatePicker({
change: DateFinPrevueChange
}).data("kendoDatePicker");
var DateFinReele = $("#DateFinReele").kendoDatePicker({
change: DateFinReeleChange
}).data("kendoDatePicker");
DateDebut.max(DateFinPrevue.value());
DateFinPrevue.min(DateDebut.value());
DateFinReele.min(DateDebut.value());
var DateFinReele = $("DateFinReele").kendoDatePicker({
change: checkDateInpuWithTodays
}).data("kendoDatePicker");
DateFinReele.max(new Date().valueOf());
}); < /script>
can anybody help me

You can actually compare JavaScript dates very easily just using normal operators.
Each Date object is really just a representation of the number of milliseconds since Jan 1, 1970. You can see a Date's time value using the getTime method.
var april212016 = new Date('2016-04-21');
console.log(april212016.getTime()); // 1461196800000
var wayWayBack = new Date('1970-01-01');
console.log(wayWayBack.getTime()); // 0
As such, when comparing dates, behind the scenes it just compares those numbers:
var d1 = new Date('2016-03-01');
var d2 = new Date('2016-01-01');
console.log(d1 > d2); // true
console.log(d2 < d1); // true
console.log(d1 == d2); // false

okeyyy ! thanks everyone for your help and your time !
i finally solved the problem with kendo validator
i just changed this code
var DateFinReele = $("#DateFinReele").kendoDatePicker({
change: DateFinReeleChange,
max: new Date()
}).data("kendoDatePicker");

Related

disable button while retrieving data from local storage and updating the date input

I am trying to implement a function that checks if the date and time I choose have already been chosen before, if so, it gives an error, if not it lets me carry on with adding the ticket. The current function does save options to local storage, however it looks like it doesn't retrieve them for me and allows me to choose the same date twice.
var count = 0;
function store() {
let clickCounter = localStorage.getItem("clickCount");
if (!clickCounter) {
localStorage.setItem("clickCount", 0);
clickCounter = "0";
}
clickCounter = parseInt(clickCounter);
document.getElementById('inc').value = ++count;
if (clickCounter == 100) {
console.log("Limit reached");
return;
} else {
localStorage.setItem("clickCount", clickCounter + 1);
}
console.log("Saving");
var e = document.getElementById("time");
var time = e.options[e.selectedIndex].text;
var date = document.querySelector("input").value;
localStorage.setItem(JSON.stringify(time), date);
console.log(localStorage);
if (!localStorage.getItem("history")) {
localStorage.setItem("history", "[]");
}
const history = JSON.parse(localStorage.getItem("history"));
history.push({ [JSON.stringify(time)]: date });
localStorage.setItem("history", JSON.stringify(history));
console.log(localStorage.getItem("history"));
}
function myDate(date) {
var today = new Date();
var newDate = new Date(date);
var nextWeek = new Date();
var pastWeek = new Date();
nextWeek.setDate(nextWeek.getDate() + 7);
pastWeek.setDate(pastWeek.getDate() - 7);
const dateInput = document.getElementById('date');
if (newDate < pastWeek || newDate > nextWeek) {
document.getElementById("time").disabled = true;
console.log("error")
return;
} else {
document.getElementById("time").disabled = false;
}
}

perform redirection if event time plus 4 hours is more than current time in js

There is a variable which contains event time. I want to redirect user if event time + 04:38 is more than current time.
Below is the code i have tried:
var deadline = getDayInstance("Monday","08:00:59")
function getDayInstance(day,time) {
const days = {"Sunday":0,"Monday":1,"Tuesday":2,"Wednesday":3,"Thursday":4,"Friday":5,"Saturday":6};
if(days[day]!==undefined)
var dayINeed = days[day];
else
var dayINeed = 2; // for Tuesday
const today = moment().tz("America/Los_Angeles").isoWeekday();
var dt;
// if we haven't yet passed the day of the week that I need:
if (today <= dayINeed) {
dt = moment().tz("America/Los_Angeles").isoWeekday(dayINeed).format('YYYY-MM-DD')+"T"+time;
}
else {
dt = moment().tz("America/Los_Angeles").add(1, 'weeks').isoWeekday(dayINeed).format('YYYY-MM-DD')+"T"+time;
}
console.log("Event Time: "+dt);
var maxLimit = Date.parse(moment(time,'HH:mm:ss').tz("America/Los_Angeles").add({"hours":4,"minutes":43,"seconds":33}).format("YYYY-MM-DDTHH:mm:ss"));
var now = Date.parse(moment().tz("America/Los_Angeles").format('YYYY-MM-DDTHH:mm:ss'));
if(maxLimit < now)
{
dt = moment().tz("America/Los_Angeles").add(1, 'weeks').isoWeekday(dayINeed).format('YYYY-MM-DD')+"T"+time;
window.setVideo = false;
}
console.log(moment(time,'HH:mm:ss').tz("America/Los_Angeles").add({"hours":4,"minutes":43,"seconds":33}).format("YYYY-MM-DDTHH:mm:ss"));
console.log(moment().tz("America/Los_Angeles").format('YYYY-MM-DDTHH:mm:ss'));
return dt;
}
var maxLimit = Date.parse(moment(deadline,'YYYY-MM-DDTHH:mm:ss').add({"hours":4,"minutes":43,"seconds":33}).format("YYYY-MM-DDTHH:mm:ss"));
var now = Date.parse(moment().tz("America/Los_Angeles").format('YYYY-MM-DDTHH:mm:ss'));
if(maxLimit>=now){
var redirectTo = $("#lp-pom-button-673").attr('href');
if(redirectTo.length > 3){
window.location.href = redirectTo;
}
else{
window.location.href = "https://******/";
}
}
I need to maintain timezone in calculation.
I got the answer of this after refining the process theoretically and then try to implement it in JS. Below is the new code that can be used for this.
var deadline = getDayInstance("Tuesday", "02:32:00");
var maxLimit,now;
function getDayInstance(day,time) {
const days = {"Sunday":0,"Monday":1,"Tuesday":2,"Wednesday":3,"Thursday":4,
"Friday":5,"Saturday":6};
if(days[day]!==undefined)
var dayINeed = days[day];
else
var dayINeed = 2; // for Tuesday
const today = moment().tz("America/Los_Angeles").isoWeekday();
var dt;
// if we haven't yet passed the day of the week that I need:
if (today <= dayINeed) {
dt = moment().tz("America/Los_Angeles").isoWeekday(dayINeed)
.format('YYYY-MM-DD')+"T"+time;
}
else {
dt = moment().tz("America/Los_Angeles").add(1, 'weeks')
.isoWeekday(dayINeed).format('YYYY-MM-DD')+"T"+time;
}
var d = new Date(dt);
d.setHours(d.getHours() + 4);
d.setMinutes(d.getMinutes() + 43);
d.setSeconds(d.getSeconds() + 32);
max = Date.parse(d);
now = Date.parse(moment().tz("America/Los_Angeles").format('YYYY-MM-DDTHH:mm:ss'));
if(maxLimit < now)
{
dt = moment().tz("America/Los_Angeles").add(1, 'weeks')
.isoWeekday(dayINeed).format('YYYY-MM-DD')+"T"+time;
}
return dt;
}
if(maxLimit>=now){
var redirectTo = $("#lp-pom-button-673").attr('href');
if(redirectTo.length > 3){
window.location.href = redirectTo;
}
else{
window.location.href = "https://******/";
}
}

How to create dynamic array for each object datetimepicker

Plugin: eonasdan/Bootstrap 3 Datepicker
Explaination:
I want to write an application that user can select 5 different hours for each day. so i created an array and add each day (without duplicate), for example user select 31/12/2017 and 30/12/2017 , now i want to give they this ability to select only 5 different hour for each day that selected.
Tried Code:
var limit = 5;
var i = 0;
var dateArray = new Object();
$('#ss').click(function() {
if ($('#datetimepicker1 input').val().length > 0) {
var date = $('#datetimepicker1 input').val();
var getDate = date.split(' ');
var unqdate = getDate[0];
var unqtime = getDate[1];
if ($.inArray(unqdate, dateArray) == -1) {
dateArray[unqdate] = unqtime
}
}
console.log(dateArray);
});
JSFiddle
(For testing, select a date, then click on save button, then check console)
Goal:
var dateArray = {
"31-12-2017": [{
"time": "14:00"
}, {
"time": "17:15"
}],
"30-12-2017": [{
"time": "13:00"
}, {
"time": "12:15"
}]
}
Problem:
I couldn't figured out how can i add another time to each day. it's my first time i work with array and object like this.
I want to prevent duplicate entry and only five different hour per day.
Somehow I'm in learning.
There is some problem with your JS Code:
var limit = 5;
var i = 0;
var dateArray = new Object();
$('#ss').click(function() {
if ($('#datetimepicker1 input').val().length > 0) {
var date = $('#datetimepicker1 input').val();
var getDate = date.split(' ');
var unqdate = getDate[0];
var unqtime = getDate[1];
if ($.inArray(unqdate, dateArray) == -1) {
if(dateArray[unqdate] && dateArray[unqdate].length < limit) {
if(!dateArray[unqdate].find((ele) =>ele.time === unqtime)){
dateArray[unqdate].push({"time": unqtime})
}
} else {
dateArray[unqdate] = [{"time": unqtime}]
}
}
}
console.log(dateArray);
});
Note included logic for time split. You can use split by : and take the first 2 element of array.
I have created JSON response for your requirement.
Result :: JSON.stringify(parentObject)
Code is as below.
$(document).ready(function() {
$('#datetimepicker1').datetimepicker({
stepping: 30,
sideBySide: true,
showTodayButton: true,
format: 'DD-MM-YYYY HH:mm:ss',
});
// my code
var limit = 5;
var i = 0;
var parentObject = new Object();
var parentArray = [];
var dateAndTimeObject;
function isDateSelectedExists(date) {
for (var i = 0; i < parentArray.length; i++) {
var obj = parentArray[i];
if (obj["date"] === date) {
return i;
}
}
return -1;
}
$('#ss').click(function() {
if ($('#datetimepicker1 input').val().length > 0) {
var date = $('#datetimepicker1 input').val();
var getDate = date.split(' ');
var unqdate = getDate[0];
var unqtime = getDate[1];
var tempIndex = isDateSelectedExists(unqdate);
console.log("tempIndex :: " + tempIndex);
if (tempIndex == -1) {
console.log("date doesn't exists");
dateAndTimeObject = new Object();
dateAndTimeObject["date"] = unqdate;
var timeArray = [];
timeArray.push(unqtime);
dateAndTimeObject["time"] = timeArray;
parentArray.push(dateAndTimeObject);
parentObject["res"] = parentArray;
} else {
console.log("date exists");
dateAndTimeObject = parentArray[tempIndex];
var timeArray = dateAndTimeObject["time"];
if(timeArray.length<5) timeArray.push(unqtime);
dateAndTimeObject["time"] = timeArray;
}
console.log("final res :: " + JSON.stringify(parentObject));
}
});});

how to test if a value is greater than another value in an array. javascript

i'm trying to do a function in javascript inorder to catch a file that have the last date. My problème is how to compare the date value that is found in an array. I have tried the code below
//indentifiant[0] is an id that i caught in the file name
//all_response is the content of the file
var timeSusDat = stats.mtime + all_response;
if (filesPerUser[identifiant[0]]) {
filesPerUser[identifiant[0]].push(timeSusDat);
} else {
var testtab = [timeSusDat];
filesPerUser[identifiant[0]] = testtab;
};
function onlyLastDate(table) {
for (var d in table) {
id = table[d];
for (var db in table[d]) {
data = table[d][db];
date = data.split('/');
var testDate = new Date(date[0]).getTime();
console.log(testDate);
}
}
}
function rangeDate(testDate){
var dateStart = new Date($('#dateStart').val()).getTime();
var dateEnd = new Date($('#dateEnd').val()).getTime();
if (dateStart <= testDate && testDate <= dateEnd) {
date = true;
return date;
}else{
date = false;
return date;
}
}

To get the date format by customizing it

I am using a js function where i can parse only 2 different date formats.If a user wants some other Date format its not taking.
function getFormattedDate(id) {
var start = document.getElementById(id).value;
var jsDate = new Date(start);
var shortWeekday = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
var fullWeekday = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var date1="";
<% String dateFormat1 = (String) session.getAttribute("calendarDateFormat");%>
var df = '<%= dateFormat1%>';
if(df.indexOf("EEEE") != -1) {
date1 = fullWeekday[jsDate.getDay()]+' '+jsDate.getMonth()+' '+jsDate.getDate()+' '+jsDate.getFullYear();
} else {
date1 = shortWeekday[jsDate.getDay()]+' '+jsDate.getMonth()+' '+jsDate.getDate()+' '+jsDate.getFullYear();
}
document.getElementById(id).value = date1;
}

Categories