I have 2 fields in HTML:
<input id="datum" type="date">
<input id="uhrzeit" type="time">
JavaScript:
var datumUhrzeit = new Date($("#datum").val()+","+$("#uhrzeit").val());
console.log(datumuhrzeit);
"Tue Aug 18 2015 16:45:00 GMT+0200 (Mitteleuropäische Sommerzeit)"
How can I convert "Tue Aug 18 2015 16:45:00 GMT+0200 (Mitteleuropäische Sommerzeit)" in PHP to a DateTime, so that I can save it to postgresql?
You can get unix timestamp from Date object as follows (see Date.prototype.getTime)
var timestamp = '#' + Math.round(datumUhrzeit.getTime()/1000);
Then when sent on server simply create new datetime object
$datumUhrzeit = new DateTime($timestamp);
If you can't use javascript to create timestamp and you get the the data from form directly you can do something like this, remember to set the timezone:
$datum = $_GET['datum'];
$uhrzeit = $_GET['uhrzeit'];
$datumUhrzeit = DateTime::createFromFormat('Y-m-d H:i:s', $datum . ' ' . $uhrzeit, new DateTimeZone('Europe/Berlin'));
Now as you have saved your date to the database and retrieved it, you can send it back
print $datumUhrzeit->format('U'); // This will print the time as unix timestamp
After that you would create your javascript date object with just the timestamp
var datumUhrzeit = new Date(timestamp * 1000); // timestamp from above
If you for some reason don't want to use unix timestamp you can print it in desired format with format method. Remember to set the timezone beforehand
$datumUhrzeit->setTimezone(new DateTimeZone('Europe/Berlin'));
print $datumUhrzeit->format('Y-m-d H:i:s');
Because javascript doesn't work well with timezones I would advocate you to use unix timestamps when you can. This way you have less problems with timezones.
You can use this javascript function to convert the dateObject or date string to your desired format:
/**
* Formats a dateObject or date string to Y-m-d date
* Example: Converts dateObject or date string Sat Aug 19 2017 00:00:00 GMT+0530 (India Standard Time) TO 2017-08-19
*/
function format_date( date )
{
if (typeof date == "string")
{
date = new Date(date);
}
var year = date.getFullYear();
var month = (1 + date.getMonth()).toString();
month = month.length > 1 ? month : '0' + month;
var day = date.getDate().toString();
day = day.length > 1 ? day : '0' + day;
return year+'-'+month+'-'+day;
}
var dateString = 'Tue Aug 18 2015 16:45:00 GMT+0200 (Mitteleuropäische Sommerzeit)';
var formattedDate = format_date(dateString);//returned formatted date is 2015-08-18
Then you can pass this formatted date to your PHP code where you can use function strtotime to convert this date to your desired format. For ex:
$myFormattedDate = date('d-m-Y', strtotime($_REQUEST['formattedDate']));
You can do something like
$datumUhrzeit = 'Tue Aug 18 2015 16:45:00 GMT+0200 (Mitteleuropäische Sommerzeit)';
$datumUhrzeit = substr($datumUhrzeit, 0, strpos($datumUhrzeit, '('));
$resultDate = date('Y-m-d h:i:s', strtotime($datumUhrzeit));
echo $resultDate;
try this one
function myFunction() {
var content = document.getElementById("datum").value+","+document.getElementById("uhrzeit").value;
console.log(content);
}
Related
I am trying to change an ISO date to a Standard JS Date format. The JS format I am referring to is:
Mon `Jul 20 2020 14:29:52 GMT-0500 (Central Daylight Time)`
What is the best way to go about doing this? Thanks!
const ISO_DATE = '2020-07-14T23:02:27.713Z';
function formatDate(dateStr) {
const date = new Date(dateStr);
return date.toString();
};
console.log(formatDate(ISO_DATE));
One way is:
let isoDate = "2020-07-20T14:29:52Z";
var myDate = new Date(isoDate);
console.log(myDate.toString()); // Mon Jul 20 2020 17:29:52 GMT+0300 ( (your time zone)
console.log("Back to ISO Date: ", myDate .toISOString());
If you want to convert it back to ISO Date use:
console.log(myDate.toISOString());
I am attempting to convert the following into a 12 hour am/pm format.
Currently I am recieving the Day, Month, Year and timezone.
Fixed by adding .toLocaleTimeString().replace(/([\d]+:[\d]{2})(:[\d]{2})(.)/, "$1$3")*
<div id="time1"></div>
<div id="time2"></div>
var date = new Date('08/16/2019 12:00:00 PM UTC').toLocaleTimeString().replace(/([\d]+:[\d]{2})(:[\d]{2})(.*)/, "$1$3")
document.getElementById("time1").innerHTML = date;
var date = new Date('08/16/2019 6:00:00 am UTC').toLocaleTimeString().replace(/([\d]+:[\d]{2})(:[\d]{2})(.*)/, "$1$3")
document.getElementById("time2").innerHTML = date;
Basically what you have to do is use the Date() default javascript function and make sure you append the UTC timezone:
var date = new Date('08/16/2019 7:00:00 PM UTC')
date.toString=() //will then print out the timezone adjusted time
"Fri Aug 16 2019 22:00:00 GMT+0300 (Eastern European Summer Time)"
There are many built in javascript methods to handle converting date objects. This example will look to the browser to determine date format and time.
let time = Date.now();
time.toLocaleDateString();
I have two date strings and a timezone (which will vary for each user). With this information I need to construct two date objects.
//information I have
var date1 = '05/05/2018'
var date2 = '06/05/2018'
var timezone = 'Australia/Sydney'
//date objects
var date1 = new Date(date1); // Sat May 05 00:00:00 GMT+00:00 2018
var date2 = new Date(date2); // Tue Jun 05 00:00:00 GMT+00:00 2018
Problem
date2 should be 6th of May (rather than 5th of June).
Since I have the timezone, is there a javascript function that will allow me to pass the date along with the timezone and it to automatically determine the correct date format (e.g dd/mm or mm/dd)?
I think the simple way to work with momentjs,
You can find example like this:
var a = moment.tz("2013-11-18 11:55", "America/Toronto");
var b = moment.tz("May 12th 2014 8PM", "MMM Do YYYY hA", "America/Toronto");
var c = moment.tz(1403454068850, "America/Toronto");
When I convert from date to String, my date is changing. What is the my fault?
Date is returned me:
Date 2016-10-31T22:00:00.000Z
And when I convert to String I get:
Thu Dec 01 2016 00:00:00 GMT+0200
My code:
date = new Date("2016 11 31");
StringDate = date.toString();
console.log(StringDate);
console.log(date);
You're after UTC time, which can be printed with Date.toUTCString():
var date = new Date("2016 11 31");
var stringDate = date.toUTCString(); // See this line
console.log(stringDate);
console.log(date);
Actually your input is wrong.There is no such date 2016-11-31. Thats why it shows Dec 01 2016.
Try this:
date=new Date("2016 10 31"); //november consist 30 days only
StringDate=date.toString();
console.log(StringDate);
console.log(date);
I have following datetime value in a json :
Fri Jan 22 2016 14:34:38 GMT-0500
I would like to display something like "January 22, 2016"
How could I achieve this in javascript. I have JQuery, Extjs libraries available.
Try creating object having properties of abbreviated months, values of full month, using for..in loop , String.prototype.slice(), String.prototype.replace()
var months = {
"Jan":"January",
"Feb":"February",
"Mar":"March",
"Apr":"April",
"May":"May",
"Jun":"June",
"Jul":"July",
"Aug":"August",
"Sep":"September",
"Oct":"October",
"Nov":"November",
"Dec":"December"
};
var date = "Fri Jan 22 2016 14:34:38 GMT-0500";
// extract "Jan 22 2016" from `date`
var d = date.slice(4, -18);
for (var prop in months) {
if (new RegExp(prop).test(d)) {
// replace abbreviated month with full month name
d = d.replace(prop, months[prop]);
// replace day with day followed by comma `,` character
d = d.replace(/(\d{2})(?=\s)/, "$1,")
}
}
document.body.textContent = d
This question is addressed to the same question of yours. You can use the functions shown here to construct the date string as you want.
// This could be any Date String
var str = "Fri Feb 08 2013 09:47:57 GMT +0530 (IST)";
var date = new Date(str);
This will then give you access to all the Date functions (MDN)
For example:
var day = date.getDate(); //Date of the month: 2 in our example
var month = date.getMonth(); //Month of the Year: 0-based index, so 1 in our example
var year = date.getFullYear() //Year: 2013
Extract date and time from string using Javascript
Found this crazy method here, but worked!
Converting milliseconds to a date (jQuery/JS)
Here is the fiddle that i'v done
https://jsfiddle.net/Ripper1992/hj6L2Lvz/
var now = new Date("Fri Jan 22 2016 14:34:38 GMT-0500");
alert(now.customFormat( "#MMMM# #DD#, #YYYY#" ) );
customFormat is the function called to get each part of the Data, parse and replace based on the #MMMM# or #DD# or #SS# defined by the user.
And here is the complete function with the documentation
http://phrogz.net/JS/FormatDateTime_JS.txt