【问题标题】:How to format a date time with timezone in Jquery?如何在 Jquery 中用时区格式化日期时间?
【发布时间】:2016-08-18 09:54:56
【问题描述】:

如何以这种格式格式化从服务器返回的日期

2016-08-17T11:17:57+04:00 以人类可读的格式 - 例如

17-08-2016, 11:17 AM

在 jquery 中?

我试过这个solution,但问题是它总是在 IST 中转换时间。甚至来自美国的人也在 IST 中看到了这一点。不太明白是什么问题。

试过了,但问题仍然存在。

Changing Date Time Format using jquery/javascript

【问题讨论】:

标签: javascript jquery ruby-on-rails postgresql


【解决方案1】:

试试这个Getting only selected date in bootstrap-date paginator

如果您不想要名称中的月份,只需保留月份值,而不是名称

另外,我认为这就是你想要的 获取日期试试这个

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
	

	
</body>

<script type="text/javascript">
  var datestring = "2016-08-17T11:17:57+04:00";
  var mydate = new Date(datestring);
  var thedate = mydate.getDate();
  var month = mydate.getMonth();
  var year = mydate.getFullYear();
 


 
  var theresult = year +"-"+"0"+(month+1)+"-"+thedate;
  alert(theresult);

   

</script>
</html>

【讨论】:

  • 当我运行这个 sn-p 时,结果是 17-08-2016, 12:47。但我要 2016-08-17, 11:17:57
  • 这个日期是自动生成的,如new Date() 还是给定的日期
  • 这个日期是我给的。
  • 你不能用这种格式给它Thu Aug 18 2016 15:52:24 GMT+0530 (IST)
【解决方案2】:

版本 1:

function pad(num) {
  return String("0"+num).slice(-2);
}
function formatDate(d) {
   day = d.getDate();
 month = d.getMonth()+1,
  year = d.getFullYear();
  return ""+pad(day)+"-"+pad(month)+"-"+year+" "+d.toLocaleTimeString();
}
var d = new Date("2016-08-17T11:17:57+04:00");
console.log(formatDate(d));

版本 2:

function chopString(d) {
  var parts = d.split("T");
  return parts[0] + " " + parts[1].split("+")[0];
}
console.log(chopString("2016-08-17T11:17:57+04:00"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    相关资源
    最近更新 更多