In my form, I have a textbox and I a field called as "pickup_date". I am using jquery Datepicker. The date is in format of dd/mm/yyyy. But when I am trying to recreate the date using JavaScript, the date is incorrectly shown as the month and the month is shown as the date. How to fix this? 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 date and time value as:
Tue Jul 03 2012 09:00:00
It is supposed to show up as
Wed Mar 07 2012 09:00:00
How to fix this?
dates are something of a weakness when working with native javascript. I feel that using a library like momentjs helps a lot with working with dates.
Look up how to make TIMESTAMPS and work with those, convert your date to a timestamp, and convert it back to a date in your desired format when you wish to print it
functions in javascript:
get current timestamp
new Date().getTime(); //in miliseconds
print timestamp to string
//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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With