简体   繁体   English

如何从JavaScript或c#中删除日期/时间字符串中的秒数?

[英]How do I remove the seconds from a date/time string in either JavaScript or c#?

I have the option of using either JavaScript or c# to accomplish this task. 我可以选择使用JavaScript或c#来完成此任务。

I have a lot of strings in the format "10/14/2012 8:45:34 PM" How do I remove the seconds and retain the PM like so "10/14/2012 8:45PM"? 我有很多字符串格式为“10/14/2012 8:45:34 PM”如何删除秒并保留PM,如此“10/14/2012 8:45 PM”?

I can't use substring because the length of the string will be different with each day and time. 我不能使用子字符串,因为字符串的长度将随着每天和每个时间而不同。

In C#, you can do this: 在C#中,您可以这样做:

myTime.ToString("MM/dd/yyyy hh:mmtt");

Alternatively, you can use the built in Format Specifiers described here . 或者,您可以使用此处描述的内置Format Specifiers

Edit - I removed the space between mm and tt because I just noticed your post shows 8:45PM . 编辑 - 我删除了mmtt之间的空格,因为我刚注意到你的帖子显示在8:45PM You can add a space between mm and tt if you want it to be 8:45 PM . 如果您希望它在8:45 PM ,可以在mmtt之间添加一个空格。

If the string is consistently formatted, then: 如果字符串格式一致,则:

var s = '10/14/2012 8:45:34 PM';
s = s.replace(/:\d+ (\w\w)$/, '$1'); // 10/14/2012 8:45PM

I hope that is only going to be used by a very limited audience since that format will be misunderstood by the vast majority of people. 我希望这只会被非常有限的观众使用,因为这种形式会被绝大多数人误解。 A more widely understood and much less unambiguous format is: 更广泛理解且更不明确的格式是:

2012-10-14 8:45PM

You can use a regular expression. 您可以使用正则表达式。

C#: C#:

string d = "10/14/2012 8:45:34 PM";
d = Regex.Replace(d, @":\d\d ", String.Empty);

Javascript: 使用Javascript:

var d = "10/14/2012 8:45:34 PM";
d = d.replace(/:\d\d /, '');

You can make use of Regular Expression for string manipulation and is pretty easier. 您可以使用正则表达式进行字符串操作,并且非常简单。

 var mydate = "10/14/2012 8:45:34 PM"; var formated_date = mydate.replace(/(\\d{1,2})\\/(\\d{1,2})\\/(\\d{4})\\ (\\d{1,2}):(\\d{1,2}):(\\d{1,2})\\ (.*)/, "$1/$2/$3\\ $4:$5$7"); document.write(formated_date); 

well, you could try: 好吧,你可以尝试:

var string="10/14/2012 8:45:34 PM";
var resultArray = string.split(":");
var result = string.replace(resultArray[2],"");

which should work effectively in javascript. 哪个应该在javascript中有效工作。 ill test it out in a second, and update this. 在一秒钟内测试它,并更新它。

UPDATE: 更新:

var string="10/14/2012 8:45:34 PM";
var resultArray = string.split(":");
var result = string.replace(":"+resultArray[2]," ")+resultArray[2].split(" ")[1];

A tad yucky. 有点难过。 but it works just fine: http://jsfiddle.net/x8uUW/ 但它的工作正常: http//jsfiddle.net/x8uUW/

UPDATE using regex (i used a regex generator, so it is not as compact as it could be, by any means. but it does work: http://jsfiddle.net/x8uUW/1/ UPDATE使用正则表达式(我使用正则表达式生成器,所以它不是那么紧凑,无论如何。但它确实有效: http//jsfiddle.net/x8uUW/1/

我尝试在Axure JavaScript中使用ToString()来设置小部件的文本,但是失败了,Axure中的正则表达式是不可能的(至少在我发布这个时),所以我想出了另一个简单的替代方案,但效率低下这样做的方法:

aDate.toLocaleTimeString().replace(':'+aDate.getSeconds(),'')

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

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