Date comparison in Jquery - javascript

I want to compare the selected date and current date. Iam using jquery date picker. While using the following code only the date field comparison is takes place... How can i compare with month and year also... Advance thanks..
$(document).ready(function() {
var objDate = new Date();
var selectedDate = document.getElementById("<%=txtRqstDate.ClientID %>").value;
var currentDate = $.datepicker.formatDate('dd/mm/yy', new Date());
if (Date.parse(selectedDate) > Date.parse(currentDate )) {
alert("Invalid date range!\n Please choose a date that is not later than today!")
return false;
}
else {
return true;
}

Pls use this.I thing it will solve ur problem
function ChDate(){
var objDate = new Date();
var selectedDate = document.getElementById("<%=txtRqstDate.ClientID %>").value;
if (selectedDate > objDate ) {
//do
}
else
{
//do
}
}
Pls chk the following link to get more functions
http://www.w3schools.com/jsref/jsref_obj_date.asp
It is possible to take split an do checking by using string.split(separator, limit)

Related

Moment JS from yyyy-mm-ddthh:mm:ss to MM-DD-YY mm:ss [duplicate]

I want to parse the following string with moment.js 2014-02-27T10:00:00 and output
day month year (14 march 2014)
I have been reading the docs but without success
http://momentjs.com/docs/#/parsing/now/
I always seem to find myself landing here only to realize that the title and question are not quite aligned.
If you want a moment date from a string:
const myMomentObject = moment(str, 'YYYY-MM-DD')
From moment documentation:
Instead of modifying the native Date.prototype, Moment.js creates a wrapper for the Date object.
If you instead want a javascript Date object from a string:
const myDate = moment(str, 'YYYY-MM-DD').toDate();
You need to use the .format() function.
MM - Month number
MMM - Month word
var date = moment("2014-02-27T10:00:00").format('DD-MM-YYYY');
var dateMonthAsWord = moment("2014-02-27T10:00:00").format('DD-MMM-YYYY');
FIDDLE
No need for moment.js to parse the input since its format is the standard one :
var date = new Date('2014-02-27T10:00:00');
var formatted = moment(date).format('D MMMM YYYY');
http://es5.github.io/#x15.9.1.15
moment was perfect for what I needed. NOTE it ignores the hours and minutes and just does it's thing if you let it. This was perfect for me as my API call brings back the date and time but I only care about the date.
function momentTest() {
var varDate = "2018-01-19 18:05:01.423";
var myDate = moment(varDate,"YYYY-MM-DD").format("DD-MM-YYYY");
var todayDate = moment().format("DD-MM-YYYY");
var yesterdayDate = moment().subtract(1, 'days').format("DD-MM-YYYY");
var tomorrowDate = moment().add(1, 'days').format("DD-MM-YYYY");
alert(todayDate);
if (myDate == todayDate) {
alert("date is today");
} else if (myDate == yesterdayDate) {
alert("date is yesterday");
} else if (myDate == tomorrowDate) {
alert("date is tomorrow");
} else {
alert("It's not today, tomorrow or yesterday!");
}
}
How to change any string date to object date (also with moment.js):
let startDate = "2019-01-16T20:00:00.000";
let endDate = "2019-02-11T20:00:00.000";
let sDate = new Date(startDate);
let eDate = new Date(endDate);
with moment.js:
startDate = moment(sDate);
endDate = moment(eDate);
Maybe try the Intl polyfill for IE8 or the olyfill service ?
or
https://github.com/andyearnshaw/Intl.js/

How can I get next three working days in JS

I'm new to typescript. I wrote this code to get the next three working days(Sunday is not a working day). But it is not working. Can someone suggest an alternative solution?
today = new Date()
day1 = get_next_working_day(today)
day2 = get_next_working_day(day1)
day3 = get_next_working_day(day2)
function get_next_working_day(day){
const temp = new Date(day.setDate(day.getDate()+1) )
if(temp.getDay() == 0){
return get_next_working_day(temp)
}
return temp
}
When you call setDate on a date, it modifies the date object itself. So your code will always change today's date to be tomorrow. Try this instead
function get_next_working_day(day){
const temp = new Date(day);
temp.setDate(temp.getDate()+1);//Modifies temp valur
if(temp.getDay() == 0){
return get_next_working_day(temp);
}
return temp;
}

Check If date is over a 2 weeks old

I’m trying to create a if statement that checks a date against the current date and throw a error if its less that 2 weeks old.
My current code
const moment = require('moment')
const today = moment().format()
const createdDate = '2020-07-30'
if (createdDate <= today ) {
console.log('Good')
} else {
console.log('Bad')
};
console.log(today)
Would appreciate any suggestions.
You can use moments subtract feature along with isSameOrAfter.
There is another option where you add two weeks to the createdDate and compare that today as well. Whichever makes more sense for you.
const today = moment();
const createdDate = moment("2020-08-30")
if (today.subtract(2, 'weeks').isSameOrAfter(createdDate)) {
console.log('good')
} else {
console.log('bad')
}
You can do this two ways and you can choose what suit you better. Both solution below will perfectly for your scenario.
You can simply use moment diff function by getting the difference of days in numbers and if the difference is more then or equal to 14 days then its good else it will be bad.
Using diff function
Live Demo:
var today = moment().startOf('day')
var createdDate = moment("2020-07-30", "YYYY-MM-DD").startOf('day')
var diff = today.diff(createdDate, 'days')
if (diff >= 14) {
console.log('Good')
} else {
console.log('Bad')
};
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.js"></script>
Using add function and clone function get the results
You can use add function along with clone and using startOf day function to make sure we always get the date from day start not when when performed the operation to check for comparison.
Live Demo:
let today = moment().startOf('day').format('YYYY-MM-DD') //today
let createdDate = moment('2020-07-30', 'YYYY-MM-DD').clone().add(14, 'days').startOf('day').format('YYYY-MM-DD') //created date minus two weeks
if (createdDate <= today) {
console.log('Good')
} else {
console.log('Bad')
};
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.js"></script>
You can use add feature of Moment as well.
const today = moment();
const createdDate = moment("2020-07-30")
if (createdDate.add(2, 'weeks').isSameOrAfter(today)) {
console.log('good to go!')
} else {
console.log('bad')
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js" integrity="sha256-ZsWP0vT+akWmvEMkNYgZrPHKU9Ke8nYBPC3dqONp1mY=" crossorigin="anonymous"></script>

How to validate timestamp in js

I need to check fox example 2117-09-15 00:00:41.0.
I saw similar topics but this var valid = (new Date(timestamp)).getTime() > 0;don't work.
If you want to check that a date is valid in MySQl then you could check that it is between some dates (min and max of mySql timestamp):
function isValid(dateString) {
var minDate = new Date('1970-01-01 00:00:01');
var maxDate = new Date('2038-01-19 03:14:07');
var date = new Date(dateString);
return date > minDate && date < maxDate;
}
Here is a fiddle for you to test: https://jsfiddle.net/x5hmyyrz/3/
As seen here: Checking if a date is valid in javascript, maybe you can use :
var date= new Date(timestamp)
valid = (date instanceof Date && !isNaN(date.valueOf()));

comparing date value in inputbox using if else in javascript

my code below is in javascript, $('#date_start').change(function(){ is working fine, but when im using if else statement to compare the date inputted in my inputbox from the current date, nothing happens at all.
$(document).ready(function() {
$('#date_start').change(function(){
var startdate = $('#date_start').datepicker("getDate");
var today = new Date();
var tomorrow = today.add(2).day();
if(startdate.getTime() < today.getTime()){
document.getElementById('finish').disabled = true;
}
else{
Remove Time from both of Date and compare it.
String input = "2012/01/20 12:05:10.321";
DateFormat inputFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
Date date = inputFormatter.parse(input);
DateFormat outputFormatter = new SimpleDateFormat("MM/dd/yyyy");
String output = outputFormatter.format(date);

Categories

Resources