Get clientside Date and Time format in Javascript - 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.

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.

javascript function to accept multiple date formats

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

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/

How to change date format using jQuery?

I have a date in a format like this fecha2.value = '2014-01-06', but I want to change the format to this '01-06-14' using jQuery.
How can I do this?
You can use date.js to achieve this:
var date = new Date('2014-01-06');
var newDate = date.toString('dd-MM-yy');
Alternatively, you can do it natively like this:
var dateAr = '2014-01-06'.split('-');
var newDate = dateAr[1] + '-' + dateAr[2] + '-' + dateAr[0].slice(-2);
console.log(newDate);
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
curr_year = curr_year.toString().substr(2,2);
document.write(curr_date+"-"+curr_month+"-"+curr_year);
You can change this as your need..
You don't need any date-specific functions for this, it's just string manipulation:
var parts = fecha2.value.split('-');
var newdate = parts[1]+'-'+parts[2]+'-'+(parseInt(parts[0], 10)%100);
I dont think you need to use jQuery at all, just simple JavaScript...
Save the date as a string:
dte = fecha.value;//2014-01-06
Split the string to get the day, month & year values...
dteSplit = dte.split("-");
yr = dteSplit[0][2] + dteSplit[0][3]; //special yr format, take last 2 digits
month = dteSplit[1];
day = dteSplit[2];
Rejoin into final date string:
finalDate = month+"-"+day+"-"+year
In most projects you want to use moment.js library along with datepicker and datetimepickers .
It doesnt look that verbose. For example
let dateString_ = moment('2023-01-17T13:39:48.530801Z').format("YYYY-MM-DD HH:mm");
for 2023-01-17 18:39
Formatting rules are here

Categories

Resources