8

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

Omar
  • 32,302
  • 9
  • 69
  • 112
vini
  • 4,657
  • 24
  • 82
  • 170
  • @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 Jul 15 '14 at 12:38

3 Answers3

13

There 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"
Maksym Kozlenko
  • 10,273
  • 2
  • 66
  • 55
  • 1
    I agree with what @MaksymKozlenko said; [`moment.js`](http://momentjs.com) will severely simplify what you need to accomplish. – royhowie Jul 14 '14 at 00:44
  • can convert datetime-local to datetime via moment.js? – vini Jul 14 '14 at 14:22
  • This answer will not work with the mobile datetime-local input type since it does not accept the time zone addition. To format for datetime-local you would need: `moment.utc("2013-07-31T05:05").local().format('YYYY-MM-DDThh:mm')` – Mattijs Dec 16 '14 at 15:43
  • The format @Mattijs is the way to go but it needs to be in 24h format or it gets screwy in the afternoon. `moment.utc("2013-07-31T05:05").local().format("YYYY-MM-DDTHH:mm")` – 1kmonkies Aug 04 '16 at 17:23
3

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 :-)

Community
  • 1
  • 1
peter_the_oak
  • 3,529
  • 3
  • 23
  • 37
3

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/

Glen Balliet
  • 1,097
  • 2
  • 12
  • 21