简体   繁体   English

如何在SQLite3中将日期时间字符串转换为UNIX时间戳?

[英]How can I convert a datetime string to a UNIX timestamp in SQLite3?

The documentation for SQLite3 datatypes says SQLite3数据类型的文档

the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values SQLite的内置日期和时间函数能够将日期和时间存储为TEXT,REAL或INTEGER值

I want to use those functions to store INTEGER values. 我想使用这些函数来存储INTEGER值。 How do I do so? 我该怎么办? The documentation for SQLite3 datetime functions describes the types of the arguments to those functions, but says nothing of the return type. SQLite3 datetime函数的文档描述了这些函数的参数类型,但没有提到返回类型。 It seems that the return type is text : 似乎返回类型是text

sqlite> select typeOf(datetime('2014-12-12 12:12:12.000'));
text

This is not what I want -- I want an integer value representing that time as a UNIX timestamp. 这不是我想要的 - 我想要一个整数值来表示该时间作为UNIX时间戳。 Even when I create a column of type integer and attempt to store a datetime(...) value in it, SQLite stores it as a text value in that integer column. 即使我创建了一个integer类型的列并尝试在其中存储datetime(...)值,SQLite也将其作为text值存储在该integer列中。

How do I force the datetime function and friends to return a UNIX timestamp, instead of a text value? 如何强制datetime函数和朋友返回UNIX时间戳而不是text值?

All the built-in date/time functions return strings. 所有内置日期/时间函数都返回字符串。

To convert a string into a number, use CAST : 要将字符串转换为数字,请使用CAST

SELECT CAST(strftime('%s', 'now') AS INT);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将字符串转换为 Python 中的 function? - How can I convert a string to a function in Python? 如何将字符串转换为 Ufunc? - How Can I Convert a String to a Ufunc? 如何在 hive 中将 unix 纪元时间转换为日期字符串 - how to convert unix epoch time to date string in hive 在PHP中将ISO 8601时间戳转换为多维数组中的Unix时间戳 - Convert iso 8601 timestamps to unix timestamp in multidimensional array in PHP 如何在PHP中将查询字符串转换为较短的字母数字字符串(然后再次转换回)? - How can I convert a query string into a a shorter alphanumeric string (and convert it back again) in PHP? 如果小数位数已知,如何将字符串(“longint”)转换为浮点数? - How can I convert a string (“longint”) to float if the number of decimals are known? Sqlite3 添加ID - Sqlite3 adding ID 有没有像sqlite3“函数”这样的东西? - Is there such a thing as an sqlite3 “function”? 我无法在 Python 中将字符串对象转换为字符串 - I can't convert a string object to string in Python 如何将使用javascript中的toLocaleString()转换的字符串转换回Date对象? - How can I convert a string converted using toLocaleString() in javascript back to Date Object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM