Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
101
rated 0 times [  103] [ 2]  / answers: 1 / hits: 22228  / 9 Years ago, sun, october 18, 2015, 12:00:00

I have a HTML input box set to type=date



<input name=datereceived type=date class=FormDateValues id=datereceived />


I want to use Jquery to fill this input box on document load to today's date but because I am in Australia, I have to offset using GMT+10



To get the date, I do the following:



var newDate = new Date();


and then I tried to set the input box using the following:



$('#datereceived').val(newDate.toLocaleDateString());


The browser tells me that for type=date, the format must be set to yyyy-MM-dd and not the Australian time format of dd/MM/yyyy.



I am not sure how to go about enforcing the toLocaleDateString AND converting the date to yyyy-MM-dd.


More From » jquery

 Answers
9

Try using String.prototype.split() , Array.prototype.splice() , Array.prototype.join()





// create array of values returned by `.toLocaleDateString()`,
// delimited by `/`
var d = new Date().toLocaleDateString().split(/);
// `y` : year
var y = d.splice(-1)[0];
// set `y` as item at index `0` of `d`
d.splice(0, 0, y);
// join items within `d` with dash character `-`
var date = d.join(-);
console.log(date);




[#64691] Thursday, October 15, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
katelynn

Total Points: 378
Total Questions: 86
Total Answers: 108

Location: Azerbaijan
Member since Fri, May 12, 2023
1 Year ago
;