Javascript 在javascript中将日期从'Thu Jun 09 2011 00:00:00 GMT+0530(印度标准时间)'转换为'YYYY-MM-DD'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6291225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert date from 'Thu Jun 09 2011 00:00:00 GMT+0530 (India Standard Time)' to 'YYYY-MM-DD' in javascript
提问by Gnanendra
How can I convert the time from the format "Thu Jun 09 2011 00:00:00 GMT+0530 (India Standard Time)" to YYYY-MM-DD.
如何将时间从格式“Thu Jun 09 2011 00:00:00 GMT+0530(印度标准时间)”转换为 YYYY-MM-DD。
When I try to alert() the date then this will show the date like the following - Thu Jun 09 2011 00:00:00 GMT+0530 (India Standard Time)
当我尝试 alert() 日期时,这将显示如下日期 - Thu Jun 09 2011 00:00:00 GMT+0530(印度标准时间)
But I need the time in YYYY-MM-DD format.
但我需要 YYYY-MM-DD 格式的时间。
Are there any built function to convert?
是否有任何内置函数可以转换?
回答by Andy E
You can parse the date using the Date
constructor, then spit out the individual time components:
您可以使用Date
构造函数解析日期,然后吐出各个时间组件:
function convert(str) {
var date = new Date(str),
mnth = ("0" + (date.getMonth() + 1)).slice(-2),
day = ("0" + date.getDate()).slice(-2);
return [date.getFullYear(), mnth, day].join("-");
}
console.log(convert("Thu Jun 09 2011 00:00:00 GMT+0530 (India Standard Time)"))
//-> "2011-06-08"
As you can see from the result though, this will parse the date into the local time zone. If you want to keep the date based on the original time zone, the easiest approach is to split the string and extract the parts you need:
但是,正如您从结果中看到的,这会将日期解析为本地时区。如果您想根据原始时区保留日期,最简单的方法是拆分字符串并提取您需要的部分:
function convert(str) {
var mnths = {
Jan: "01",
Feb: "02",
Mar: "03",
Apr: "04",
May: "05",
Jun: "06",
Jul: "07",
Aug: "08",
Sep: "09",
Oct: "10",
Nov: "11",
Dec: "12"
},
date = str.split(" ");
return [date[3], mnths[date[1]], date[2]].join("-");
}
console.log(convert("Thu Jun 09 2011 00:00:00 GMT+0530 (India Standard Time)"))
//-> "2011-06-09"
回答by rachwongrw
The easiest way for me to convert a date was to stringify it then slice it.
我转换日期的最简单方法是将其字符串化然后对其进行切片。
var event = new Date("Fri Apr 05 2019 16:59:00 GMT-0700 (Pacific Daylight Time)");
let date = JSON.stringify(event)
date = date.slice(1,11)
// console.log(date) = '2019-04-05'
回答by Richard - IMPI MEDIA
function convert(str) {
var date = new Date(str),
mnth = ("0" + (date.getMonth()+1)).slice(-2),
day = ("0" + date.getDate()).slice(-2);
hours = ("0" + date.getHours()).slice(-2);
minutes = ("0" + date.getMinutes()).slice(-2);
return [ date.getFullYear(), mnth, day, hours, minutes ].join("-");
}
I used this efficiently in angular because i was losing two hours on updating a $scope.STARTevent, and $scope.ENDevent, IN console.log was fine, however saving to mYsql dropped two hours.
我在 angular 中有效地使用了它,因为我在更新 $scope.STARTevent 和 $scope.ENDevent, IN console.log 上浪费了两个小时,但是保存到 myYsql 减少了两个小时。
var whatSTART = $scope.STARTevent;
whatSTART = convert(whatever);
THIS WILL ALSO work for END
这也适用于 END
回答by Dhaval Chheda
Above solutions will work only if its a string. Input type date, gives you output in javascript date objectin some cases like if you use angular or so. That's why some people are getting error like "TypeError: str.split is not a function". It's a date object, so you should use functions of Date object in javascript to manipulate it. Example here:
只有当它是一个string 时,上述解决方案才有效。输入类型日期,在某些情况下为您提供 javascript日期对象的输出,例如您使用 angular 左右。这就是为什么有些人会收到诸如“TypeError: str.split is not a function”之类的错误。它是一个日期对象,因此您应该使用 javascript 中的 Date 对象的函数来操作它。这里的例子:
var date = $scope.dateObj ;
//dateObj is data bind to the ng-modal of input type dat.
console.log(date.getFullYear()); //this will give you full year eg : 1990
console.log(date.getDate()); //gives you the date from 1 to 31
console.log(date.getMonth() + 1); //getMonth will give month from 0 to 11
Check the following link for reference:
检查以下链接以供参考:
回答by Moses Ndeda
function convertDatePickerTimeToMySQLTime(str) {
var month, day, year, hours, minutes, seconds;
var date = new Date(str),
month = ("0" + (date.getMonth() + 1)).slice(-2),
day = ("0" + date.getDate()).slice(-2);
hours = ("0" + date.getHours()).slice(-2);
minutes = ("0" + date.getMinutes()).slice(-2);
seconds = ("0" + date.getSeconds()).slice(-2);
var mySQLDate = [date.getFullYear(), month, day].join("-");
var mySQLTime = [hours, minutes, seconds].join(":");
return [mySQLDate, mySQLTime].join(" ");
}