简体   繁体   English

隐藏日期字符串到datetime

[英]Covert date string to datetime

I have my date string as 17-06-2017 and I want to create it in format 2017-06-14 10:49:50 to insert in mysql database, nut I'm not able to do it like : 我的日期字符串为17-06-2017 ,我想以2017-06-14 10:49:50格式创建它以插入到mysql数据库中,但我无法做到:

var now = new Date(2017-06-17).toISOString().slice(0, 19).replace('T', ' ');

Gives

"1970-01-01 00:00:01"

And

var now = new Date(17-06-2017).toISOString().slice(0, 19).replace('T', ' ');

Gives

"1969-12-31 23:59:57" ; "1969-12-31 23:59:57" ;

Need some accurate calculations here, don't understand what I'm doing wrong. 这里需要一些准确的计算,不明白我在做什么错。

将日期括入这样的字符串中:

new Date("2017-06-17")

Well what you are doing inside the brackets here is a simple subtraction: new Date(17-06-2017) this will spit out a negative number. 好吧,您在括号内的操作是一个简单的减法: new Date(17-06-2017)这将吐出一个负数。 What you want to do is something like that: 您想要做的是这样的:

 var now = new Date(2017,06,17).toISOString().slice(0, 19).replace('T', ' '); console.log(now) 

I would recommend using moment.js , it is the best library to work with dates , format , timezones. 我建议使用moment.js,它是处理date,format,timezones的最佳库。

let now = new Date();
let formattedDate = moment(now).format("YYYYMMDD HHMISS");

你可以用片刻做

var date=moment(yourdate).format(yourformat)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM