Date format fix

In my form, I have a text box and I call it "pickup_date". I am using jquery Datepicker. The date is in the format dd / mm / yyyy. But when I try to recreate a date using JavaScript, the date is not displayed correctly as a month and the month is displayed as a date. How to fix it? My current script and values ​​are below:

<script language="javascript" type="text/javascript">
var dateStart = '07/03/2012'; //dd/mm/YYYY format
var timeStart = '09:00';

var startDate = new Date(dateStart + " " + timeStart);

document.write(startDate);

</script>

      

The above gives the datetime value as:

Tue Jul 03 2012 09:00:00

      

It is supposed to display as

Wed Mar 07 2012 09:00:00

      

How to fix it?

+3


source to share


5 answers


Ok after a lot of hacking, I finally came up with my own solution without using any external complex computation libraries. So far, it seems to me that it works fine. Below is the code:

<script language="javascript" type="text/javascript">

var dateStart = '07/03/2012';
var timeStart = '09:00';

var startDate = new Date(dateStart + " " + timeStart);

document.write(startDate);
document.write('<br />');

//declare an array
var my_arr = Array();
//explode/split the dd/mm/yyyy string so that we can collect it in our array
my_arr = dateStart.split('/');
//join the pieces of array in our mm/dd/yyyy format
var revised_date = my_arr[1] + '/' + my_arr[0] + '/' + my_arr[2];
//Generate the complete date
var startDate_revised = new Date(revised_date + " " + timeStart);
//Output the date
document.write(startDate_revised);
</script>

      



And thanks to everyone for their input. I really appreciate all the help.

0


source


dates are a bit of a weakness when dealing with native javascript. I feel like using a library like momentjs helps when dealing with dates.



+1


source


See how to make TIMESTAMPS and work with them, convert date to timestamp and convert it back to the date you want when you want to print it.

functions in javascript:

get the current timestamp

new Date().getTime(); //in miliseconds

      

print timestamp on line

//make formated date
var date = new Date(unix_timestamp*1000);
// hours part from the timestamp
var hours = date.getHours();
// minutes part from the timestamp
var minutes = date.getMinutes();
// seconds part from the timestamp
var seconds = date.getSeconds();

// will display time in 10:30:23 format
var formattedTime = hours + ':' + minutes + ':' + seconds;

      

string to timestamp

//this only supports limited formats
function toTimestamp(strDate){
 var datum = Date.parse(strDate);
 return datum/1000;
}

function toTimestamp(year,month,day,hour,minute,second){
 var datum = new Date(Date.UTC(year,month-1,day,hour,minute,second));
 return datum.getTime()/1000;
}

      

+1


source


Javascript uses American date form mm/dd/YYYY

0


source


Take a look at globalize.js - this will give you a lot more control over how you control the date-to-string conversion so that your software can more easily accommodate the different needs of a custom format based on culture.

Internationalization and localization in JavaScript is still very problematic.

0


source







All Articles