Javascript Text Field / Date Parser - javascript

I am trying to a add a parser to a text field to make sure the user enters a date with the format MM/DD/YYYY. I am also using a Calendar Picker to manipulate state as well, and updating the text-box with moment.js ... however, I need to add a parser for when the user is typing the date, where they can type the / but also the / adds itself when needed.
A few scenarios with a before/after idea of parser:
3 -> 03/
3161995 -> 03/16/1995
31/ -> 03/01/

you can make use following function
function formatDate(date) {
let d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [month, day, year].join('-');
}
/* pass the data as follows
console.log(formateDate(new Date())); */

Related

How to get tomorrow's date of a specific date

I am trying to get tomorrow's date of a specific date using JavaScript in format (yyyy-mm-dd). For example the specific date is 2021-08-31 and I have got this script:
var date = "2021-08-31"
date = new Date(date.split("-")[0],date.split("-")[1],date.split("-")[2])
date.setDate(date.getDate() + 1);
var tomorrows_date_month = date.getMonth()
var tomorrows_date_day = date.getDate()
var tomorrows_date_year = date.getFullYear()
console.log(tomorrows_date_year + "-" + tomorrows_date_month + "-" + tomorrows_date_day)
The expected output is:
2021-09-01
But the output of this code is :
2021-9-2
First you don't need split "2021-08-31" to use as date parameter, so just use new Date("2021-08-31");
Second note that you need to use d.getMonth() + 1 and add leading zero if the length is less than 2:
Try this one:
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [year, month, day].join('-');
}
Date.prototype.addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}
var date = "2021-08-31"
var date1 = new Date(date);
console.log(formatDate(date1.addDays(1)));
Internally js month is stored as a value between 0 and 11. So you need to minusdate.split("-")[1] by 1. Otherwise, javascript will think that your month is actually September and we know that "2021-09-32" is translated to "2021-10-2", therefore the date is shown as "2".
var date = "2021-08-31"
date = new Date(date.split("-")[0],date.split("-")[1] - 1,date.split("-")[2])
date.setDate(date.getDate() + 1)
var tomorrows_date_month = date.getMonth() + 1
var tomorrows_date_day = date.getDate()
var tomorrows_date_year = date.getFullYear()
console.log(tomorrows_date_year + "-" + tomorrows_date_month + "-" + tomorrows_date_day)
Also note that date = new Date("2021-08-31") is enough for converting a string into a Date object.
new Date(new Date(date + 'T00:00Z').getTime() + 86400000).toISOString().substr(0, 10)
The added 'T00:00Z' assures the date is parsed as UTC, to match the UTC timezone used by toISOString(). Adding 86400000 (the number of milliseconds in one day) advances the date without having to fuss with the date field directly.

Remove time part from date in js

let date = invoice.due_date;
console.log(date);
Output 2019-06-13 00:00:00
d = date.split(' ')[0]; //didnt work for me
How can I remove the time and only have the date.
I just added .toLocaleDateString
The toLocaleDateString() method returns a string with a language-sensitive representation of the date portion of the date. The locales and options arguments let applications specify the language whose formatting conventions should be used and allow to customize the behavior of the function.
let date = new Date("2019-06-13T02:00:00Z").toLocaleDateString()
console.log(date)
Reference:
toLocaleDateString
Another Example:
If you want to have a ISO Date try this one:
date = new Date('2019-06-13T02:00:00Z');
year = date.getFullYear();
month = date.getMonth() + 1;
dt = date.getDate();
if (dt < 10) {
dt = '0' + dt;
}
if (month < 10) {
month = '0' + month;
}
console.log(year + '-' + month + '-' + dt);
let date = invoice.due_date;
console.log(date.getDate() + '-' + (date.getMonth()+1) + '-' + date.getFullYear());
You can try this way. Can create any format like dd-MM-yyyy or anything.
Recommendation: Use moment library for date formatting.
If you had a string, the split would work.
It is either not a string (e.g. null) or something else not a string.
Your console.log shows a date string so it is obviously a Date object.
To get the second part in ANY case (space or with a T between the date and time) you need to get the ISOString to be able to PERSISTENTLY get the correct output.
Any toLocaleString or similar is implementation and locale dependent
let date = invoice.due_date.toISOString()
Like this:
// Assuming a date object because your console log and the split that does not work
const invoice = {
due_date : new Date("2019-06-13 00:00:00") // EXAMPLE date
}
let date = invoice.due_date.toISOString();
console.log(date)
console.log(date.split(/[T| ]/)[0]); // take space or "T" as delimiter
You can convert the date string to a Date Object:
let dataObj = new Date(date)
and then format it as given in this link

web api not receiving date from angularjs

Hi I am developing web application in angularjs. I have one date in angularjs for example 13-10-2017. In c# i have below field
public DateTime LicenseExpiryDate { get; set; }
When i send 13-10-2017 in ajax request,LicenseExpiryDate accepts as 0001-01-01. May i know how to fix this? I can see my angular date is in dd-mm-yy format and c# date default format is yyyy-mm-dd.
i tried to convert it to yyyy-mm-dd as below
function formatDateDOsetyymmdd(date) {
debugger;
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year,month, day].join('-');
}
and this returns NaN-NaN-NaN
Any help would be appreciated. Thank you.
You are getting a string in form dd-mm-yyyy on input, what you want to do is turn that string into three numbers, and use those in the new Date call
function formatDateDOsetyymmdd(date) {
date = date.split('-').map(Number);
var d = new Date(date[2], date[1] - 1, date[0]),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year,month, day].join('-');
}
console.log(formatDateDOsetyymmdd('13-10-2017'));
you can use moment.js , if you can't do this in JavaScript means you can use it.
https://momentjs.com/

change date format in displaying

I have this below code where i am trying to alert the date selected on click of an image. I am getting the alert but the issue is date format.
Below is alert i am getting. I need the alert in mm/dd/yyy.
And also why i am getting Thu Jan 01 1970 if i use todatestring() method.
Below is the javascript code
Try this function to return mm/dd/yyyy
function getmmDdYy(thisDate) {
return ('0' + (thisDate.getMonth() + 1)).slice(-2) +
'/' +
("0" + (thisDate.getDate())).slice(-2) +
'/' +
thisDate.getFullYear();
}
So on your fnUpdateDate(), do this:
function fnUpdateDate(date1,date2){
alert(getmmDdYy(new Date(date1)) +" ss "+ getmmDdYy(new Date(date2)));
}
Let me know if this works.
You can retrieve the single components from a date (day, month, year) and build a string with them (https://stackoverflow.com/a/30272803).
If you add the library moment.js you will be able to use formatting strings like YYYYMMDD (https://stackoverflow.com/a/27635223).
You can pass your date to this function and return the formate as per your need
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return (month+"/"+day+"/"+year);
}

Selenium IDE - function checkDate (Current date and date in the future)

In my user extensions i used this code to check the date but it keeps getting errors:
Selenium.prototype.doCheckDate = function(){
var dates = new Date();
var day = dates.getDate();
if (day < 10){
day = '0' + day;
}
month = dates.getMonth() + 1;
if (month < 10){
month = '0' + month;
}
var year = dates.getFullYear();
var prettyDay = day + '-' + month + '-' + year;
this.browserbot.getUserWindow().checkDate(prettyDay);
}
The error: [error] this.browserbot.getUserWindow(...).checkDate is not a function
Does anybody have an idea how i can check the current date or dates that are in the future? Our systems fills a datefield with a date that is one or two years from the current date.
Thanks for your time and effort in advance. I appreciate it.
It appears that all your function does is gets the current date. What is your ultimate goal? Do you intend to compare a the system date to a date from the field and verify it's 1 or 2 years later?

Categories

Resources