我想将以下对象中的日期时间分别拆分为日期和时间。
/root
Dockerfile
docker-compose.yml
docker_resource.txt
config
src/
project-files.whatever
root-Files/
root-files.txt
并将其保存在两个不同的变量中。
答案 0 :(得分:3)
如果您不希望有任何意外,可以使用moment.js库。
moment(A.datetime).format('L'); // get date only
moment(A.datetime).format('LTS') // get time only.
使用momentjs
时,您还可以具有特定的日期和时间格式。
您也可以为此使用JavaScript的Date class。
const date = new Date(A.datetime);
//to get date
date.getDate() +' '+date.getMonth()+' '+date.getFullYear();
//to get time.
date.getHours()+' '+date.getMinutes()+' '+date.getSeconds();
答案 1 :(得分:2)
例如,您可以使用split()
var splitString = A.datetime.split(" ");
// Returns array, ['2019-02-01','15:30:43']
您可以使用splitString[0]
作为日期和splitString[1]
作为时间来访问它。