问题描述
我的时间格式为:12/16/2011 3:49:37 PM,我通过以下方式获取此格式:
I have a time format like: 12/16/2011 3:49:37 PM and I got this format by:
var newDate = new Date(timeFromat);
timeFormat = newDate.toLocaleString();
我的实际格式是格林尼治标准时间(GMT),我使用上面的代码将其转换为凉亭时间.
My actual format was GMT and I converted it to my bowers time by using the code above.
,我想将其更改为24小时制,因此我希望将此日期更改为:12/16/2011 15:49:37,我想使用javascript.
and I want to change it to 24h time format so I want this date to be changed like: 12/16/2011 15:49:37 and I want to do it in javascript.
那是我所做的
var firstPartOftimeFormat = timeFormat.substring(0,9);
var secondPartOftimeFormat = timeFormat.substring(10,20);
,但当日期格式为:3/16/2011时,则不起作用.但以下部分有效.
but it does not work when the date format is like: 3/16/2011. but the following part works.
var time = $("#starttime").val();
var hours = Number(secondPartOftimeFormat.match(/^(\d+)/)[1]);
var minutes = Number(secondPartOftimeFormat.match(/:(\d+)/)[1]);
var AMPM = secondPartOftimeFormat.match(/\s(.*)$/)[1];
if(AMPM == "PM" && hours<12) hours = hours+12;
if(AMPM == "AM" && hours==12) hours = hours-12;
var sHours = hours.toString();
var sMinutes = minutes.toString();
if(hours<10) sHours = "0" + sHours;
if(minutes<10) sMinutes = "0" + sMinutes;
alert(sHours + ":" + sMinutes);
您可以建议其他方法吗?
Can you suggest other approach?
谢谢
推荐答案
使用 dateObj.toLocaleString([locales [,options]])
选项1 -使用语言环境
var date = new Date();
console.log(date.toLocaleString('en-GB'));
选项2 -使用选项
var options = { hour12: false };
console.log(date.toLocaleString('en-US', options));
这篇关于在javascript中将时间格式更改为24小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!