Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 340
vini Avatar asked Jul 11 '14 06:07

vini


1 Answers

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"
like image 63
Maksym Kozlenko Avatar answered Sep 20 '22 18:09

Maksym Kozlenko