How to convert datetime-local to datetime while storing and retrieving from database

15,683

Solution 1

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"

Solution 2

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

Solution 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/

Share:
15,683
vini
Author by

vini

UI Developer

Updated on June 20, 2022

Comments

  • vini
    vini almost 2 years

    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

  • royhowie
    royhowie almost 10 years
    I agree with what @MaksymKozlenko said; moment.js will severely simplify what you need to accomplish.
  • vini
    vini almost 10 years
    can convert datetime-local to datetime via moment.js?
  • Mattijs
    Mattijs over 9 years
    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')
  • 1kmonkies
    1kmonkies almost 8 years
    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")