我的时间格式如下:12/16/2011 3:49:37 PM我得到的格式为:
var newDate = new Date(timeFromat);
timeFormat = newDate.toLocaleString();
我的实际格式是格林尼治标准时间,我使用上面的代码将其转换为我的预订时间。
我希望将其更改为24小时格式,因此我希望此日期更改为:12/16/2011 15:49:37并且我想在javascript中执行此操作。
这就是我做的事情
var firstPartOftimeFormat = timeFormat.substring(0,9);
var secondPartOftimeFormat = timeFormat.substring(10,20);
但是当日期格式如下时它不起作用:3/16/2011。但以下部分有效。
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);
你能建议其他方法吗?
感谢
答案 0 :(得分:11)
使用System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
psi.FileName = @"cmd";
psi.Arguments = "/C start notepad.exe";
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
System.Diagnostics.Process.Start(psi);
选项1 - 使用区域设置
dateObj.toLocaleString([locales[, options]])
选项2 - 使用选项
var date = new Date();
console.log(date.toLocaleString('en-GB'));
答案 1 :(得分:2)
您可以使用以下代码获取24小时格式
new Date("3/16/2011 3:49:37 PM").getHours() // 15
new Date("3/16/2011 3:49:37 PM").getMinutes() // 49
答案 2 :(得分:2)
获取 24 小时格式:
const date = new Date();
console.log(date.toLocaleTimeString([], {
hourCycle: 'h23',
hour: '2-digit',
minute: '2-digit'
}));
答案 3 :(得分:1)
试试这个:
// British English uses day-month-year order and 24-hour time without AM/PM
console.log(date.toLocaleString('en-GB'));
// → "20/12/2012 03:00:00"
参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
答案 4 :(得分:1)
尝试一下,它对我来说完全正常。 它提供了24小时的时间格式
var Date= new Date();
var TimeFormat= date.toLocaleString('en-GB');
您的答案将是 2018年12月14日星期五18:00 GMT + 0530(印度标准时间)
答案 5 :(得分:1)
hourCycle: 'h23'
是完美的
感谢https://stackoverflow.com/users/12425695/luis-serrano
new Date().toLocaleString('ru-RU', {
timeZone: 'Europe/Moscow',
hourCycle: 'h23',
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit"
});
答案 6 :(得分:0)
尝试此功能
<script type="text/javascript">
<!--
function displayTime() {
var currentDate = new Date();
var currentHour = currentDate.getHours();
var currentMinute = currentDate.getMinutes();
var currentSecond = currentDate.getSeconds();
document.getElementById('timeDiv').innerHTML = 'Hour: ' + currentHour + ' Minute: ' + currentMinute + ' Second: ' + currentSecond;
}
window.onload = function() {
window.setInterval('displayTime()', 1000);
}
// -->
</script>
答案 7 :(得分:0)
试试这个
var d = new Date();
alert(d.getHours());
alert(d.getMinutes());
答案 8 :(得分:0)
Using some resources
Date/toLocaleDateStringMDN
Date/toLocaleTimeStringMDN
const lang = navigator.language || navigator.languages[0];
const date = new Date();
const date_locale = date.toLocaleDateString(lang, {
day: 'numeric',
month: 'short',
year: 'numeric'
});
const time_locale = date.toLocaleTimeString(lang);
const formatted = `${date_locale} ${time_locale}`;
console.log(formatted)
above we deduce the current language from the Window's Navigator object.
In case lang
ends up being undefined
it's perfectly fine, defaults will be used.
To force a desired format, you can manually set lang
to i.e: 'en-US'
, 'eu'
, 'en-GB'
, 'de-DE'
, 'hr-HR'
etc...
Here's an example for time:
const date = new Date();
console.log(date.toLocaleTimeString('en-US')) // 12h
console.log(date.toLocaleTimeString('en-GB')) // 24h
console.log(date.toLocaleTimeString()) // Default