javascript function to accept multiple date formats - javascript

Users want to use the following formats to enter dates:-
mm-dd-yyyy OR yyyy-mm-dd OR m-d-yy OR m-d-yyyy OR mm/dd/yyyy OR m/d/yy.
My plan is to capture whatever they enter and convert it to yyyy-mm-dd because that is the format that the date field value must be submitted. They have refused to use a calendar . I have tried the following JS function without any success. Any ideas?
var value = ctrl.getValue();
var date_input = new Date(value);
var day = date_input.getDay();
var month = date_input.getMonth() + 1;
var year = date_input.getFullYear();
var yyyy_MM_dd = year + "-" + month + "-" + day;
return yyyy_MM_dd

Because its wrong to use var day = date_input.getDay();
use it like this
var day = date_input.getDate();
function getDateFormat(value){
var date_input = new Date(value);
var day = date_input.getDate();
var month = date_input.getMonth() + 1;
var year = date_input.getFullYear();
var yyyy_MM_dd = year + "-" + month + "-" + day;
return yyyy_MM_dd;
}
console.log(getDateFormat("2017-12-30"));
console.log(getDateFormat("12-30-2017"));
.getDay is giving you days of weeks
Also be sure to use date format accepted by Date

Related

How to change format to standard time JavaScript

I have a date string in JavaScript which is arranged in a particular manner. How do I rearrange this in order to fit standard time?
let date = "2020-06-01T00:00:00Z"
How do I rearrange the date variable in order to match the format MM/DD, YYYY ?
Change your code to as follows:
let date = "2020-06-01T00:00:00Z";
date = new Date(date);
var dd = date.getDate();
var mm = date.getMonth()+1;
var yyyy = date.getFullYear();
if(dd<10){dd='0'+dd}
if(mm<10){mm='0'+mm};
console.log(mm+'/'+dd+', '+yyyy)
i don't real know this is possible but i found the function you can use instead
function GetFormattedDate() {
var todayTime = new Date();
var month = todayTime.getMonth() + 1;
var day = todayTime.getDate();
var year = todayTime.getFullYear();
return month + "/" + day + "/" + year;
}
console.log(GetFormattedDate());
so you can add more in the function or edit what the function will return as a format according to your will

How can I get the value of a key from JSON to JS?

I'm working on an application that requires dates to be fed from JS and check in a JSON table what the "Letter" of the day is.
How can I take the JS date string that I've created (yyyy,mm,dd), and find the value of the corresponding key?
I have tried taking the string, stringifying it, and pushing it through a JS function.
var d = new Date();
var date = d.getDate();
var month = d.getMonth() + 1;
var year = d.getFullYear();
var dateStr = year + "/" + month + "/" + date;
console.log(dateStr);
dateJSON = JSON.stringify(dateStr)
console.log(dateJSON)
alert(testVar.dateJSON)
var testVar = { //In a JS file
"2019-11-06": "D",
"2019-11-08": "A_con" //continues for a very long time.....
}
For "2019-11-08" I would like to have variable "letterDay" equal "A_con".
My code thus far returns "undefined" when I pull "testVar.dateJSON"
I think it is far simpler than this, your data keys are in format yyyy-mm-dd while you're dateStr is in the format yyyy/mm/dd here is a simple snippet
var testVar = { //In a JS file
"2019-11-09": "D",
"2019-11-08": "A_con" //continues for a very long time.....
}
var d = new Date();
var date = d.getDate();
var month = d.getMonth() + 1;
var year = d.getFullYear();
// Your mistake was here, the separators were '/'
var dateStr = year + "-" + month + "-" + date;
console.log(dateStr);
// Get the value dynamically
// when you have a dynamic key use testVar[dateStr]
// testVar.dateStr will literally look for a key called "dateStr" it will not evaluate the value of dateStr
console.log(testVar[dateStr]);
You formatted your dateStr variable the wrong way ("/" instead of "-").
var testVar = { //In a JS file
"2019-11-06": "D",
"2019-11-08": "A_con" //continues for a very long time.....
}
var d = new Date('November 06, 2019 23:15:30');
var day = d.getDate();
var month = d.getMonth() + 1;
var year = d.getFullYear();
var dateStr = year + "-" + month + "-" + (day > 9 ? day : '0' + day);
console.log(testVar[dateStr]); // D
Have your tried using JSON.parse()?
So: JSON.parse(testVar); and then modify your code and variables from there.

Get clientside Date and Time format in Javascript

I want is, the formatted string as, MM/DD/YY or DD/MM/YYYY.
Which of these is set by client in his/her system.
How to get it?
var date = new Date();
var day = date.getDay();
var month = date.getMonth();
var year = date.getFullYear();
var final = day + "/" + month + "/" + year;
console.log(final); // "1/11/2015"
http://jsbin.com/zixacacuda/edit?js,console
var dt=new Date('2015-12-12');
var d=dt.getDay();
var m=dt.getMonth();
var y=dt.getFullYear();
var dtformated=d+'/'+m+'/'+y
console.log(dtformated)
var now = new Date().toLocaleDateString();
it willl retrun "12/28/2015 (MM/DD/YYYY)" format.

HTML5 get date string convert to date object

I try to use html5 type="date" and get the string and convert it to Date() in JS.
var dateString = $(this).prev().val();
var date = new Date(dateString);
var day = date.getDay();
var month = date.getMonth();
var year = date.getYear();
finalDate = day + "/" + month + "/" + year;
alert(finalDate);
But the result I got is different than what I've set, I have no idea what's wrong here :
I expect to get 18/05/1991
My demo is here http://jsfiddle.net/yL1q3ygf/
getMonth() returns a number between 0 and 11. You want to use getMonth()+1.
getDay() returns the day of the week, you want to use getDate() instead.
use getUTCDate(); before making it as an object (date).
var d = new Date(dateString);
var n = d.getUTCDate();
then use n in your code. it will work
$(function(){
$('button').click(function(){
var dateString = $(this).prev().val();
var date = new Date(dateString);
var day = date.getDate(); //day is for day of week. use date
var month = date.getMonth() +1; // months are zero based
var year = date.getFullYear(); //use full year for 4 digit year
finalDate = day + "/" + month + "/" + year;
alert(finalDate);
});
});
I updated your jsfiddle script
http://jsfiddle.net/yL1q3ygf/5/

Convert html5 date input to date object

I have a form setup with html5 date input what I wanna do is get that date add some days to it and then show the final result in another html5 date input
In my code here, the days are added to todays date.
how do I alter the Javascript so that the days are added to the user selected date.
current JS i am using:
var terms = $("#terms").val();
var date = new Date();
date.setDate(date.getDate() + terms);
var day = ("0" + date.getDate()).slice(-2);
var month = ("0" + (date.getMonth() + 1)).slice(-2);
var final = date.getFullYear()+"-"+(month)+"-"+(day);
$("#duedate").val(final);
You need to parse your terms val as an integer - parseInt(terms). Fiddle.
$('#terms').on('blur', function() {
var terms = $("#terms").val();
var date = new Date($("#date").val());
date.setDate(date.getDate() + parseInt(terms));
var day = ("0" + date.getDate()).slice(-2);
var month = ("0" + (date.getMonth() + 1)).slice(-2);
var final = date.getFullYear()+"-"+(month)+"-"+(day);
$("#duedate").val(final);
});

Categories

Resources