在 Julia 中将日期时间格式“1/22/20”转换为“2020-01-22”

时间:2021-06-24 15:23:12

标签: julia

我想知道将日期时间格式“1/22/20”转换为“2020-01-22”的最佳方法是什么。我尝试了 DateTime.(d1.date, "m/d/y"),但我觉得我在输出中遗漏了一些东西。有什么建议吗?

julia> first(d1, 10)
10×2 DataFrame
 Row │ date     value
     │ String   Int64
─────┼────────────────
   1 │ 1/22/20      1
   2 │ 1/23/20      1
   3 │ 1/24/20      2
   4 │ 1/25/20      2
   5 │ 1/26/20      5
   6 │ 1/27/20      5
   7 │ 1/28/20      5
   8 │ 1/29/20      6
   9 │ 1/30/20      6
  10 │ 1/31/20      8

julia> DateTime.(d1.date, "m/d/y")
519-element Vector{DateTime}:
 0020-01-22T00:00:00
 0020-01-23T00:00:00
 0020-01-24T00:00:00
 0020-01-25T00:00:00
 0020-01-26T00:00:00
 0020-01-27T00:00:00
 0020-01-28T00:00:00
 0020-01-29T00:00:00
 0020-01-30T00:00:00
 0020-01-31T00:00:00
 0020-02-01T00:00:00
 0020-02-02T00:00:00
 0020-02-03T00:00:00

1 个答案:

答案 0 :(得分:3)

DateTime 构造函数将字符串转换为日期时间。要转换回字符串(不同格式),请使用 Dates.format:

Dates.format.(DateTime.(d1.date, dateformat"m/d/y"), dateformat"yyyy-mm-dd")

由于您的年份只有两位数字,因此它们被解释为 20 年而不是 2020 年。要解决此问题,您可以在日期中添加 Dates.Year(2000)

Dates.format.(DateTime.(d1.date, dateformat"m/d/y") .+ Dates.Year(2000), dateformat"yyyy-mm-dd")

您应该确保尽可能使用 @dateformat_str 宏来构造 DateFormat,因为 StringDateFormat 的转换非常昂贵,并且使用宏来构造 import 'package:brew_crew/model/user.dart'; import 'package:firebase_auth/firebase_auth.dart'; class AuthService{ final FirebaseAuth _auth = FirebaseAuth.instance; // Create user object based on FirebaseUser CurrentUser? _customModelForFirebaseUser(User user) { return user != null ? CurrentUser (uid: user.uid) : null; } // auth changed user stream Stream<CurrentUser > get user { return _auth.onAuthStateChanged // here is the problem .map(_customModelForFirebaseUser); } //sin in anon Future signInAnon() async{ try { UserCredential result = await _auth.signInAnonymously(); User? user = result.user; return _customModelForFirebaseUser(user!); }catch (e) { print(e.toString()); return null; } } //sign in with E-mail and password //Register with E-mail and passqord //Sign out } do it 意味着它在编译时(当宏展开时)发生一次,而不是每个 DataFrame 行发生一次。