简体   繁体   English

如何转换日期时间以下格式?

[英]How to convert datetime following format?

I am working with sailsjs(node.js), I have get all the data from mysql database and display the data in jtable but date format is coming like this: YYYY-MM-DDTHH:mm:ss.000Z .我正在使用sailsjs(node.js),我已经从mysql数据库中获取了所有数据并在jtable中显示数据,但日期格式是这样的: YYYY-MM-DDTHH:mm:ss.000Z

I need to convert this(YYYY-MM-DDTHH:mm:ss.000Z) format into `14-08-2015 04:36:04 PM我需要将此(YYYY-MM-DDTHH:mm:ss.000Z)格式转换为`14-08-2015 04:36:04 PM

In jtable i have used following format but not working.在 jtable 中,我使用了以下格式但不起作用。

UpdatedDate: {
  edit: false,
  create: false,
  type: 'datewithtime',
  displayFormat: 'dd-mm-yy',
  Weightage: 2,
  tooltip: 'Date Modified',
  title: 'Date Modified'
  /*display: function (data) {
   return moment(data).format('YYYY-MM-DDTHH:mm:ss');
   // return data.format("dd-m-yy");
   }*/
}

in model 
  CreatedDate : { type: 'DATETIME'},

This is a simple workaround using just javascript.这是仅使用 javascript 的简单解决方法。 This may not be the best way to do this but its simple and works.这可能不是执行此操作的最佳方法,但它简单且有效。

var date = new Date("1994-11-05T08:15:30-05:00");//this is the format you have
date.toLocaleDateString().split('/').join('-')+" "+date.toLocaleTimeString() //this converts it to necessary format
OUTPUT: "11-5-1994 6:45:30 PM"

EDIT: Sorry for that I guess it converted date as per your System Locale.编辑:抱歉,我猜它根据您的系统区域设置转换了日期。 Ok well so raw fix is as follows ::好吧,原始修复如下:

var date = new Date("1994-11-05T08:15:30-05:00");//this is the format you have")
var datePart = date.getDate()+"-"+date.getMonth()+"-"+date.getFullYear();
function formatAMPM(date) { 

var hours = date.getHours();
  var minutes = date.getMinutes();
  var secs = date.getSeconds();
  var ampm = hours >= 12 ? 'PM' : 'AM';
  hours = hours % 12;
  hours = hours ? hours : 12; // the hour '0' should be '12'
  minutes = minutes < 10 ? '0'+minutes : minutes;
  var strTime = hours + ':' + minutes +':'+secs+ ' ' + ampm;
  return strTime;
}
var timePart = formatAMPM(date);
console.log(datePart+" "+timePart);

OK this time each field was extracted individually rather than using any existing format so this will work for sure.好的,这次每个字段都是单独提取的,而不是使用任何现有格式,因此这肯定会起作用。 Alternatively if you are open to using external libraries here is another solution using moment.js或者,如果您愿意使用外部库,这里是使用 moment.js 的另一种解决方案

moment(date).format("DD-MM-YYYY hh:mm:ss A")

Sample code:示例代码:

SELECT DATE_FORMAT('2007-10-04 22:23:00', '%d-%m-%Y %h:%i:%s %p')

Make following changes when selecting data from table:从表中选择数据时进行以下更改:

displayFormat: '%d-%m-%Y %h:%i:%s %p' displayFormat: '%d-%m-%Y %h:%i:%s %p'

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

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