Basic Validation in Date Time using JavaScript
var StartTime = $("select[title='StartTime'] option:selected").val();
var EndTime = $("select[title='EndTime'] option:selected").val();
var date = $('input[title="ReserveDate"]').val();
//convert both time into timestamp
var stt = new Date(date + start_time);
stt = stt.getTime();
var endt = new Date(date + end_time);
endt = endt.getTime();
//by this you can see time stamp value in console via firebug
console.log("Time1: "+ stt + " Time2: " + endt);
if(stt > endt) {
alert("time");
}
----------------------------------------------------------------------------------------------------
var stt = parseInt($("select[title='StartTime'] option:selected").val().replace(':', ''), 10);
alert(stt);
var ett = parseInt(EndTime .replace(':', ''), 10);
alert(ett);
----------------------------------------------------------------------------------------------------
var StartTime = $("select[title='StartTime'] option:selected").val();
var EndTime = $("select[title='EndTime'] option:selected").val();
var time1_higher_than_time2 = compareTime(StartTime , EndTime);
if(time1_higher_than_time2) alert("Time1 is higher than time 2");
else alert("Time1 is not higher than time 2. ");
function compareTime(StartTime, EndTime){
var re = /^([012]?\d):([0-6]?\d)\s*(a|p)m$/i;
StartTime = StartTime.match(re);
EndTime = EndTime.match(re);
if(StartTime && EndTime){
var is_pm1 = /p/i.test(StartTime[3]) ? 12 : 0;
var hour1 = (StartTime[1]*1 + is_pm1) % 12;
var is_pm2 = /p/i.test(EndTime[3]) ? 12 : 0;
var hour2 = (EndTime[1]*1 + is_pm2) % 12;
if(hour1 != hour2) return hour1 > hour2;
var minute1 = StartTime[2]*1;
var minute2 = EndTime[2]*1;
return minute1 > minute2;
}
---------------------------------------------------------------------------------------------------
var start_time = Date.parse(StartTime, "hh:mm tt"); //Convert Time from A/PM to time
var end_time = Date.parse(EndTime, "hh:mm tt"); //Convert Time from A/PM to time
var startDate = new Date("1/1/1900 " + start_time);
var endDate = new Date("1/1/1900 " + end_time);
var difftime = end_time - start_time;
-----------------------------------------------------------------------------------------------------
if(is_pm1 == 12)
{
hour1 = hour1 - 12;
}
var is_pm2 = /P/i.test(TestendTime [3]) ? 12 : 0;
alert(is_pm2);
var hour2 = ((TestendTime [1]*1) + is_pm2) ;
alert(hour2);
if(is_pm2 == 12)
{
hour2 = hour2 - 12;
}
alert(hour1 );
alert(hour2);
if(hour1==12) hour1=0;
if(hour2 == 12) hour1=0;
if(hour1 != hour2)
{
if(hour1 > hour2)
{
alert("Your StartTime is later than your End Time. Please change either the Start or End Time");
return false;
}
}
else if(minute1 == minute2)
{
alert("Your Start Time is equal to your End Time. Please change the Start Time or End Time");
return false;
}
----------------------------------------------------------------------------------------------------
References:
http://forums.asp.net/t/1289294.aspx?TimeSpan+Years+and+Months
http://stackoverflow.com/questions/4638993/difference-in-months-between-two-dates
DATETIME IN JQUERY
-----------------------------------------------------------------------------------------------------
<script type="text/javascript">
$(document).ready(function() {
var currentdate = new Date();
var h=currentdate.getHours();
if (h >= 12) {
h = h-12;
dd = "PM";
}
if (h == 0) {
h = 12;
}
var ampm=h >= 12 ? 'PM' : 'AM';
var datetime = "at: " +(currentdate.getMonth()+1 + "/"
+ currentdate.getDate() + "/"
+ currentdate.getFullYear() + " on " + (h<10 ? '0' : '') +
+ h + ":"
+ currentdate.getMinutes()+ ampm);
alert(datetime );
});
</script>
No comments:
Post a Comment