如何将用户输入的日期时间转换为毫秒

时间:2019-06-27 10:03:17

标签: django

我正在尝试将用户输入的日期时间转换为毫秒。在这里,我正在使用模板文件供用户输入日期时间。我正在提取和存储在from和to的变量中。如何将其转换为毫秒。

module SearchCommon
  extend ActiveSupport::Concern

  included do
    include Elasticsearch::Model

    after_commit on: [:create, :update] do
      __elasticsearch__.index_document
    end

    after_commit on: [:destroy] do
      __elasticsearch__.delete_document
    end

    mapping do # ADDITION 
     indexes :activity_type, type: 'keyword' # ADDITION
    end # ADDITION 

    index_name [Rails.env, model_name.collection].join('_')
  end
end

1 个答案:

答案 0 :(得分:0)

要将日期时间转换为毫秒:

Python 3:

from datetime import datetime

datetime_obj = datetime.today()
datetime_millisec = datetime_obj.timestamp() * 1000

Python 2.7:

from datetime import datetime

datetime_obj = datetime.today()
# convert into seconds and then multiply by 1000
datetime_millisec = int(datetime_obj.strftime('%s')) * 1000

用于计算日期时间差(以毫秒为单位):

from datetime import datetime, timedelta
time_diff = from_date - to_date // assuming from_date and to_date are datetime objects
time_diff_millisec = time_diff.total_seconds() * 1000