I am beating my head around making this possible how do i convert datetime-local which i am using in jquery mobile and store data as datetime as my field is datetime in the database
'<input type="datetime-local"' + demodata + ' />';
I am using jquery mobile and having major issues
if($(this).attr('type')==='datetime-local')
{
var $datevalue =$(this).val();
a[$(this).attr('name')] = $datevalue.toString(); //Have to convert to datetime instead
}
My datetime-local value is in this format: 2014-07-18T12:12
I am beating my head around making this possible how do i convert datetime-local which i am using in jquery mobile and store data as datetime as my field is datetime in the database
'<input type="datetime-local"' + demodata + ' />';
I am using jquery mobile and having major issues
if($(this).attr('type')==='datetime-local')
{
var $datevalue =$(this).val();
a[$(this).attr('name')] = $datevalue.toString(); //Have to convert to datetime instead
}
My datetime-local value is in this format: 2014-07-18T12:12
- @flup jQM tag is irrelevant to this question, even if the OP is using jQM. The question is specific and clear, it's about date-time only. – Omar Commented Jul 15, 2014 at 12:38
3 Answers
Reset to default 13 +50There is moment.js library which does lots of date time processing including time zones, date formatting, etc. It handles DST time correctly.
This code converts local time into UTC based on user's time zone settings (Australian EDT (UTC +1100) in my case):
// convert local time to UTC
moment(new Date(2014, 0, 1, 0, 0, 0)).utc().format()
// returns "2013-12-31T13:00:00+00:00"
// convert UTC to local time
moment.utc("2013-07-31T05:05").local().format()
// returns "2013-07-31T15:05:00+10:00"
Datetime with JavaScript is not an easy matter. There are many questions in SO and answers how to convert from string to date time and vice versa.
If you work on this subject, you will see these aspects, which make it complicated:
- Localization: +/- n hours geographical time shifting
- Daylight saving time (DST): yes/no, +/- 1 hour, depending on the current date
- different adaptations, age of the client browser, UTC functions available or not, etc. Be warned that not all browsers parse an ISO date time string exactly the same way.
Always try to be aware which localisation and which DST is inherently included or is silently interpreted. By working with full length ISO strings and by using detailed setters/getters, you will reduce the confusions.
So from string to Date
have a look here:
How can I convert string to datetime with format specification in JavaScript?
From Date
to string:
How do you get a timestamp in JavaScript?
You'll find much more :-)
You should be able to just create a new Date
with the value from the input:
var str = $('[type=datetime-local]').val();
var d = new Date(str);
You haven't specified a format for your timestamp, but because this is designed to be inserted into a database, I've written a convenience function which converts a JS date into a MySQL datetime
format.
JS Fiddle: http://jsfiddle.net/UujT3/3/