最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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

Share Improve this question edited Jul 15, 2014 at 12:38 Omar 31.7k9 gold badges72 silver badges115 bronze badges asked Jul 11, 2014 at 6:38 vinivini 4,72224 gold badges84 silver badges176 bronze badges 1
  • @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
Add a comment  | 

3 Answers 3

Reset to default 13 +50

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"

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/

发布评论

评论列表(0)

  1. 暂无评论