将字符串转换为特定的时间格式(字符串)JavaScript

时间:2016-07-27 10:35:49

标签: javascript

我需要在JavaScript中将字符串从html数据标记转换为特定的时间格式。

var time = "August 01, 2016 01:30" 

需要成为

"01 August 2016 01:30:00"

以下代码执行此操作:

var time = "August 01, 2016 01:30"
var time_array = time.split(",")
var month = time_array[0].split(' ')[0]
var day = time_array[0].split(' ')[1]
var year = time_array[1].split(' ')[1]
var time = time_array[1].split(' ')[2]+ ":00"
var result = day + " " + month + " " + year + " " + time

但我想知道是否有更有效,更快捷的方法来做到这一点?

5 个答案:

答案 0 :(得分:2)

假设您不需要检查输入字符串是否描述了有效的日期和时间,您只需使用.replace()方法:

var time = "August 01, 2016 01:30";

time = time.replace(/([a-z]+) (\d{2}), (\d{4} \d{2}:\d{2})/i, "$2 $1 $3:00");

console.log(time);

答案 1 :(得分:1)

如果您不想包含整个库,并且仍想使用实际日期,那么自己解析日期并不困难

var time = "August 01, 2016 01:30";

var months = [
    'January',
    'February',
    'March',
    'April',
    'May',
    'June',
    'July',
    'August',
    'September',
    'October',
    'November',
    'December'
];

var parts = time.split(/[\s,:]/);
var date  = new Date(parts[3], months.indexOf(parts[0]), parts[1], parts[4], parts[5]);

现在您有了一个日期对象,您可以输出任何您想要的内容,并且您可以使用months数组来获取月份等。

function pad(x) { return x < 10 ? '0' + x : x}

var new_date = [
    pad(date.getDate()), 
    months[date.getMonth()], 
    date.getFullYear(), 
    pad(date.getHours()) + ':' +
    pad(date.getMinutes())  + ':' +
    pad(date.getSeconds())
];

var parsed = new_date.join(' ');

var time = "August 01, 2016 01:30";

var months = [
  'January',
  'February',
  'March',
  'April',
  'May',
  'June',
  'July',
  'August',
  'September',
  'October',
  'November',
  'December'
];

var parts = time.split(/[\s,:]/);
var date  = new Date(parts[3], months.indexOf(parts[0]), parts[1], parts[4], parts[5]);

function pad(x) { return x < 10 ? '0' + x : x}

var new_date = [
    pad(date.getDate()), 
    months[date.getMonth()], 
    date.getFullYear(), 
    pad(date.getHours()) + ': ' +
    pad(date.getMinutes())  + ': ' +
    pad(date.getSeconds())
];
                    
var parsed = new_date.join(' ');

document.body.innerHTML = parsed;

答案 2 :(得分:0)

根据@andrey-etumyan的建议,您应该使用moment.js

&#13;
&#13;
var time = "August 01, 2016 01:30" 
var d = moment(time, "MMMM DD, YYYY hh:mm")
console.log(d.format("DD MMMM YYYY hh:mm:ss"))
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

使用momentjs,您可以拥有以下代码:

&#13;
&#13;
var time = "August 01, 2016 01:30";
time = moment(time, 'MMMM DD, YYYY HH:mm').format('DD MMMM YYYY HH:mm:ss');
console.log(time)//"01 August 2016 01:30:00"
&#13;
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>
&#13;
&#13;
&#13;

答案 4 :(得分:0)

您可以轻松地执行此操作,无需任何动作或手动解析字符串。日期会很好地解析它。

&#13;
&#13;
var date = new Date("August 01, 2016 01:30");
var months = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"];


console.log(date.getDate() + ' ' + months[date.getMonth()] + ' ' +
            date.getFullYear()  + ' ' + date.getHours() + ':' + date.getMinutes() );
&#13;
&#13;
&#13;