我正在使用类型为 的输入datetime-local。我需要将其转换为 unix 时间戳。
以下是提交的原始值的格式示例2018-06-12T19:30。
我需要将上述格式的日期转换为1608954764当前的 unix 时间戳格式。
我正在尝试将以下字符串转换为 python 中的日期时间。参考datetime.strptime(\xe2\x80\x982017-01-12T14:12:06.000-0500\xe2\x80\x99,'%Y-%m-%dT%H:%M:%S.%f %Z')我正在尝试下面提到的格式。
\nconfig_current_ts = datetime.strptime(internal_config["timestamp_str"], "%Y:%m:%dT%H:%M:%S.%f%z")\nRun Code Online (Sandbox Code Playgroud)\n但我收到一条错误消息:
\n\n\nValueError: 时间数据 \'2021-01-18T11:18:10.833876+00:00\' 与格式\n不匹配 \'%Y:%m:%dT%H:%M:%S.%f%z\ '
\n
我使用的是python3.7.4。有人可以告诉我如何将其转换为日期时间吗?我想基本上比较字符串和当前时间,看看哪个在前面。
\n我从商店获取日期和时间。在数据库中它看起来像这样
需要知道如何将其显示为 DD/MM/YY
我正在尝试这样做
String timeString = snapshot.data[index]['lastupdate'].toString();
DateTime date = DateTime.parse(timeString);
print(DateFormat('yyyy-MM-dd').format(date));
Run Code Online (Sandbox Code Playgroud)
它显示日期格式无效的错误
我有一个函数可以将 Unix 纪元时间解析为yyyy-MM-dd'T'HH:mm:ss.SSSXXX如下格式,以便将其导出到文件中:
public static final SimpleDateFormat REQIF_DATE_FORMAT_WITH_MILLIS
= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
public static String convertEpochStringToReqifDateString(String epochString) {
Date timestamp = new Date(Long.parseLong(epochString));
return REQIF_DATE_FORMAT_WITH_MILLIS.format(timestamp);
}
Run Code Online (Sandbox Code Playgroud)
现在,我对此导出进行了测试,但是当它们在本地通过时,它们在服务器上失败,因为它显然位于不同的时区。具体来说,差异如下:
LAST-CHANGE="2017-03-13T21:36:44.261+01:00"
LAST-CHANGE="2017-03-13T20:36:44.261Z"
Run Code Online (Sandbox Code Playgroud)
我已经在测试之前尝试运行一些东西,以确保测试始终在同一时区运行,例如:
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
Run Code Online (Sandbox Code Playgroud)
System.setProperty("user.timezone", "UTC");
Run Code Online (Sandbox Code Playgroud)
以及 JUnitPioneer 注释:
@DefaultTimeZone("UTC")
Run Code Online (Sandbox Code Playgroud)
...但是,它们似乎都没有影响解析输出。
对此我能做什么?我想要的只是某种方法来确保我的测试在同一时区运行,无论运行的机器位于何处,以便我可以正确测试导出。
System.out.println(
DateTimeFormatter.ofPattern("YYYY-ww").withZone(ZoneOffset.UTC).format(Instant.parse("2022-05-10T00:00:00.00Z"))
);
System.out.println(
DateTimeFormatter.ofPattern("YYYY-ww").withZone(ZoneOffset.UTC).format(Instant.parse("2022-05-17T00:00:00.00Z"))
);
Run Code Online (Sandbox Code Playgroud)
为什么此模式YYYY-ww在 Ubuntu 和 Mac 上的解析不同:
Ubuntu:(默认区域设置 en_US,我的电脑)
2022-20
2022-21
Run Code Online (Sandbox Code Playgroud)
Mac:(默认区域设置 en_GB)
2022-19
2022-20
Run Code Online (Sandbox Code Playgroud)
编辑
System.out.println(
DateTimeFormatter.ofPattern("YYYY-ww").withLocale(Locale.UK).withZone(ZoneOffset.UTC).format(Instant.parse("2022-05-10T00:00:00.00Z"))
);
System.out.println(
DateTimeFormatter.ofPattern("YYYY-ww").withLocale(Locale.UK).withZone(ZoneOffset.UTC).format(Instant.parse("2022-05-17T00:00:00.00Z"))
);
Run Code Online (Sandbox Code Playgroud)
返回:
2022-19
2022-20
Run Code Online (Sandbox Code Playgroud)
不过,问题是为什么模式ww是特定于区域设置的?我在https://docs.oracle.com/javase/8/docs/api/java/time/temporal/WeekFields.html
或https://docs.oracle.com/javase/8的文档中没有看到这一点/docs/api/java/time/format/DateTimeFormatter.html
Instant我正在尝试根据本文中的参考从对象创建 ISO 8601 格式的 DateTime,我使用该格式YYYY-MM-DD'T'hh:mm:ss'T'ZD来解析即时日期,如下所示。
但它生成的时间格式错误:
2022-06-172T06:08:13T-0500172
Run Code Online (Sandbox Code Playgroud)
预期的格式应该是:
2022-06-21T13:31:49-05:00
Run Code Online (Sandbox Code Playgroud)
我的代码:
DateTimeFormatter formatter = DateTimeFormatter
.ofPattern("YYYY-MM-DD'T'hh:mm:ss'T'ZD")
.withZone(ZoneId.systemDefault());
formatter.format(Instant.now())
Run Code Online (Sandbox Code Playgroud)
如何生成如下所示的格式化时间?
2022-06-21T13:31:49-05:00
Run Code Online (Sandbox Code Playgroud) 我正在尝试将一个chrono::zoned_seconds对象作为文本写入文件,然后检索它并chrono::zoned_seconds稍后构造另一个对象。如何才能以相当有效的方式完成此任务?
我认为下面的代码片段没有显示正确的结果:
#include <fstream>
#include <print>
#include <chrono>
#include <format>
#include <string>
#include <sstream>
int main()
{
{
std::ofstream file("data.txt");
if (!file) {
std::println( "Unable to open file.\n" );
return 1;
}
auto now = std::chrono::system_clock::now();
const std::chrono::zoned_seconds zs { "America/New_York", std::chrono::time_point_cast<std::chrono::seconds>( now ) };
std::format_to(std::ostreambuf_iterator<char>(file), "{:%F %T %Z}", zs);
std::println( "{:%F %T %Z}", zs ); // correct time
}
{
std::ifstream file("data.txt");
if (!file) {
std::println( "Unable to open file.\n" );
return 1;
}
std::string …Run Code Online (Sandbox Code Playgroud) 我有一个在线表单,其中包含一些包含时间数据的字段.我将这些数据存储到MySQL数据库中的一个time字段中,该字段需要一种格式hh:mm:ss.如果用户以正确的格式输入时间,那么我想接受数据.我也想允许用户输入的标准美国时间的时候,喜欢9:30 am或11:25 pm或10:27 am等
基本上我想测试时间是否是正确的数据库格式first(hh:mm:ss),然后如果不是,测试它是否是第二个接受的格式(hh:mm am/pm),如果是,那么我将使用PHP函数strtotime()进行转换它进入数据库时间格式.如果它不是这两种格式,那么我们会显示错误消息并死掉.
有谁知道如何测试变量的值是否与这些时间格式之一匹配?
我想做的伪PHP代码:
<?php
$value = //some time;
if (is_database_time($value)){
// good no problem
}else if (is_usa_time($value)){
$value = strtotime($value);
}else{
die("error incorrect time format, try again.");
}
?>
Run Code Online (Sandbox Code Playgroud)
**编辑**
谢谢大家的帮助.我在这里使用了一些信息来创建一个完美运行的函数:
<?php
function filter_time($key,$value){
// this section handles the storage of time data
if (preg_match('/^(0?\d|1\d|2[0-3]):[0-5]\d:[0-5]\d$/', $value)){
//do nothing
}else if (preg_match('/^(0?\d|1[0-2]):[0-5]\d\s(am|pm)$/i', $value)){
$value = date( 'H:i:s', strtotime($value));
}else{
display_error('incorrect …Run Code Online (Sandbox Code Playgroud) 我无法通过抛出不正确格式的异常来将字符串转换为日期:
这是patterens:
DD-MM-YYYY
MM-DD-YYYY
YYYY-DD-MM
YYYY-MM-DD
YYYYDDMM
YYYYMMDD
Run Code Online (Sandbox Code Playgroud)
格式来自下拉列表,这是我尝试不同的方法,但仍然抛出异常:
strCurrentFormat = rcboDateFormat.SelectedValue.ToString();
DateTime db = DateTime.ParseExact(strOldDate, "DD-MM-YYYY", CultureInfo.InvariantCulture);
//DateTime loadedDate = DateTime.ParseExact(strOldDate, strCurrentFormat, null);
Run Code Online (Sandbox Code Playgroud)
我之前已经解决了这个问题,通过取弦,拆分它们并将年,月和日移动到适当的格式,但这需要很长时间,如果有人知道更简单的方法,那将是一个巨大的帮助.
我有一个简单的查询,它计算两个日期时间之间的时差,如下所示:
SELECT TIMEDIFF( '2012-08-19 08:25:13', '2012-07-19 18:05:52' );
Run Code Online (Sandbox Code Playgroud)
产出:734:19:21
输出以小时为单位:min:sec.但是我希望输出格式为:
天:小时:分:秒
如何格式化输出?
datetime-format ×10
datetime ×5
java ×3
date ×2
php ×2
asp.net-4.0 ×1
c# ×1
c++ ×1
c++-chrono ×1
c++23 ×1
dart ×1
epoch ×1
flutter ×1
fstream ×1
java-8 ×1
java-time ×1
mysql ×1
python ×1
sql ×1
testing ×1
time ×1
time-format ×1
timezone ×1
unit-testing ×1