我的应用程序中有一个函数,可以将日期以字符串形式保存到数据库中,但是无法保存该日期。我已经尝试了一个简短的代码。假设我们在字符串中有一个日期。
代码:
package main
import (
"fmt"
"strconv"
"strings"
"time"
)
func main() {
p := fmt.Println
date := "01-25-2019"
arrayDate := strings.Split(date, "-")
fmt.Println(arrayDate)
month, _ := strconv.Atoi(arrayDate[0])
dateInt, _ := strconv.Atoi(arrayDate[1])
year, _ := strconv.Atoi(arrayDate[2])
then := time.Date(
year, time.Month(month), dateInt, 0, 0, 0, 0, time.UTC)
p(then)
p(then.Weekday())
}
有没有更有效的方法?
游乐场link
答案 0 :(得分:8)
是的,只需使用time.Parse()
来解析时间,例如
def get_window_data2(data, window, steps):
X = []
y = []
scaler = MinMaxScaler(feature_range=(0, 1))
data = scaler.fit_transform(data.reshape(-1, 1))
for i in range(len(data) - window - 1):
X.append(data[i : i + window])
y.append(data[i + window + 1: i + window + 1 + steps])
X = np.asarray(X)
y = np.asarray(y)
return X, y, scaler
window_size = 33
steps_size = 11
X, y, scaler = get_window_data2(data['denormalized_glucose_value'].values, window_size, steps_size)
test_split = int(data.shape[0] * 0.8)
X_train = X[:test_split]
X_test = X[test_split:]
y_train = y[:test_split]
y_test = y[test_split:]
model = Sequential()
model.add(LSTM(50, input_shape=(window_size, 1))) # 50neurons, and and window_size = 50
model.add(Dropout(0.2))
model.add(Dense(11, activation="sigmoid"))
model.compile(loss="mse", optimizer="adam")
model.fit( X_train, y_train, epochs=20, batch_size=10, validation_data=(X_test, y_test), verbose=2, shuffle=False)
date := "01-25-2019"
t, err := time.Parse("01-02-2006", date)
if err != nil {
panic(err)
}
fmt.Println(t.Weekday())
将执行您尝试手动实现的解析。请注意,time.Parse()
的第一个参数是布局字符串,它必须包含给定输入格式的参考时间(即time.Parse()
)。
输出(在Go Playground上尝试):
Mon Jan 2 15:04:05 -0700 MST 2006