Hour from DateTime? in 24 hours format
So i have this DateTime? and what i want to do is to obtain the hour but show it in 24 hours format.
For example:
If the hour is 2:20:23 p.m. i want to convert it to 14:20 and that's it.
I'm working with Visual C#.
Any ideas please, thank you.
I have something like this
public static string FormatearHoraA24(DateTime? fechaHora)
{
if (!fechaHora.HasValue)
return "";
string retornar = "";
//here goes what i need
}
c# datetime
add a comment |
So i have this DateTime? and what i want to do is to obtain the hour but show it in 24 hours format.
For example:
If the hour is 2:20:23 p.m. i want to convert it to 14:20 and that's it.
I'm working with Visual C#.
Any ideas please, thank you.
I have something like this
public static string FormatearHoraA24(DateTime? fechaHora)
{
if (!fechaHora.HasValue)
return "";
string retornar = "";
//here goes what i need
}
c# datetime
@OrelEraki are you serious? This question was posted before that one by two years. At least check your facts before calling duplicate.
– Archibald
Sep 26 '16 at 5:50
There is no need to get exited, it was on 2014. Removed it.
– Orel Eraki
Sep 26 '16 at 7:17
2
"hh" in lower case is for am/pm notation, "HH" is for 24h notation
– Antoine Meltzheim
Sep 13 '17 at 13:48
add a comment |
So i have this DateTime? and what i want to do is to obtain the hour but show it in 24 hours format.
For example:
If the hour is 2:20:23 p.m. i want to convert it to 14:20 and that's it.
I'm working with Visual C#.
Any ideas please, thank you.
I have something like this
public static string FormatearHoraA24(DateTime? fechaHora)
{
if (!fechaHora.HasValue)
return "";
string retornar = "";
//here goes what i need
}
c# datetime
So i have this DateTime? and what i want to do is to obtain the hour but show it in 24 hours format.
For example:
If the hour is 2:20:23 p.m. i want to convert it to 14:20 and that's it.
I'm working with Visual C#.
Any ideas please, thank you.
I have something like this
public static string FormatearHoraA24(DateTime? fechaHora)
{
if (!fechaHora.HasValue)
return "";
string retornar = "";
//here goes what i need
}
c# datetime
c# datetime
asked Jun 11 '10 at 14:34
euther
92651434
92651434
@OrelEraki are you serious? This question was posted before that one by two years. At least check your facts before calling duplicate.
– Archibald
Sep 26 '16 at 5:50
There is no need to get exited, it was on 2014. Removed it.
– Orel Eraki
Sep 26 '16 at 7:17
2
"hh" in lower case is for am/pm notation, "HH" is for 24h notation
– Antoine Meltzheim
Sep 13 '17 at 13:48
add a comment |
@OrelEraki are you serious? This question was posted before that one by two years. At least check your facts before calling duplicate.
– Archibald
Sep 26 '16 at 5:50
There is no need to get exited, it was on 2014. Removed it.
– Orel Eraki
Sep 26 '16 at 7:17
2
"hh" in lower case is for am/pm notation, "HH" is for 24h notation
– Antoine Meltzheim
Sep 13 '17 at 13:48
@OrelEraki are you serious? This question was posted before that one by two years. At least check your facts before calling duplicate.
– Archibald
Sep 26 '16 at 5:50
@OrelEraki are you serious? This question was posted before that one by two years. At least check your facts before calling duplicate.
– Archibald
Sep 26 '16 at 5:50
There is no need to get exited, it was on 2014. Removed it.
– Orel Eraki
Sep 26 '16 at 7:17
There is no need to get exited, it was on 2014. Removed it.
– Orel Eraki
Sep 26 '16 at 7:17
2
2
"hh" in lower case is for am/pm notation, "HH" is for 24h notation
– Antoine Meltzheim
Sep 13 '17 at 13:48
"hh" in lower case is for am/pm notation, "HH" is for 24h notation
– Antoine Meltzheim
Sep 13 '17 at 13:48
add a comment |
4 Answers
4
active
oldest
votes
You can get the desired result with the code below. Two'H' in HH
is for 24-hour format.
return fechaHora.Value.ToString("HH:mm");
3
Description as to why this works would be useful. Such as HH for 24 hour format as hh for 12 hour.
– Luke Alderton
Aug 6 '18 at 13:56
add a comment |
date.ToString("HH:mm:ss"); // for 24hr format
date.ToString("hh:mm:ss"); // for 12hr format, it shows AM/PM
Refer this link for other Formatters in DateTime.
add a comment |
Using ToString("HH:mm")
certainly gives you what you want as a string.
If you want the current hour/minute as numbers, string manipulation isn't necessary; you can use the TimeOfDay
property:
TimeSpan timeOfDay = fechaHora.TimeOfDay;
int hour = timeOfDay.Hours;
int minute = timeOfDay.Minutes;
I like this method because it gives me integer values for calculation.
– Hao Nguyen
Jan 4 '16 at 20:03
This was the preferred solution for me because I just needed the int from the hour of the day. Much easier then converting to a string just to parse back into a DateTime again.
– Sev
Nov 17 '16 at 22:22
add a comment |
Try this:
//String.Format("{0:HH:mm}", dt); // where dt is a DateTime variable
public static string FormatearHoraA24(DateTime? fechaHora)
{
if (!fechaHora.HasValue)
return "";
return retornar = String.Format("{0:HH:mm}", (DateTime)fechaHora);
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3023649%2fhour-from-datetime-in-24-hours-format%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can get the desired result with the code below. Two'H' in HH
is for 24-hour format.
return fechaHora.Value.ToString("HH:mm");
3
Description as to why this works would be useful. Such as HH for 24 hour format as hh for 12 hour.
– Luke Alderton
Aug 6 '18 at 13:56
add a comment |
You can get the desired result with the code below. Two'H' in HH
is for 24-hour format.
return fechaHora.Value.ToString("HH:mm");
3
Description as to why this works would be useful. Such as HH for 24 hour format as hh for 12 hour.
– Luke Alderton
Aug 6 '18 at 13:56
add a comment |
You can get the desired result with the code below. Two'H' in HH
is for 24-hour format.
return fechaHora.Value.ToString("HH:mm");
You can get the desired result with the code below. Two'H' in HH
is for 24-hour format.
return fechaHora.Value.ToString("HH:mm");
edited Nov 21 '18 at 8:31
Heemanshu Bhalla
2,08211233
2,08211233
answered Jun 11 '10 at 14:40
vtortola
20.3k21120206
20.3k21120206
3
Description as to why this works would be useful. Such as HH for 24 hour format as hh for 12 hour.
– Luke Alderton
Aug 6 '18 at 13:56
add a comment |
3
Description as to why this works would be useful. Such as HH for 24 hour format as hh for 12 hour.
– Luke Alderton
Aug 6 '18 at 13:56
3
3
Description as to why this works would be useful. Such as HH for 24 hour format as hh for 12 hour.
– Luke Alderton
Aug 6 '18 at 13:56
Description as to why this works would be useful. Such as HH for 24 hour format as hh for 12 hour.
– Luke Alderton
Aug 6 '18 at 13:56
add a comment |
date.ToString("HH:mm:ss"); // for 24hr format
date.ToString("hh:mm:ss"); // for 12hr format, it shows AM/PM
Refer this link for other Formatters in DateTime.
add a comment |
date.ToString("HH:mm:ss"); // for 24hr format
date.ToString("hh:mm:ss"); // for 12hr format, it shows AM/PM
Refer this link for other Formatters in DateTime.
add a comment |
date.ToString("HH:mm:ss"); // for 24hr format
date.ToString("hh:mm:ss"); // for 12hr format, it shows AM/PM
Refer this link for other Formatters in DateTime.
date.ToString("HH:mm:ss"); // for 24hr format
date.ToString("hh:mm:ss"); // for 12hr format, it shows AM/PM
Refer this link for other Formatters in DateTime.
answered Jun 11 '10 at 14:42
Manish Basantani
8,378185594
8,378185594
add a comment |
add a comment |
Using ToString("HH:mm")
certainly gives you what you want as a string.
If you want the current hour/minute as numbers, string manipulation isn't necessary; you can use the TimeOfDay
property:
TimeSpan timeOfDay = fechaHora.TimeOfDay;
int hour = timeOfDay.Hours;
int minute = timeOfDay.Minutes;
I like this method because it gives me integer values for calculation.
– Hao Nguyen
Jan 4 '16 at 20:03
This was the preferred solution for me because I just needed the int from the hour of the day. Much easier then converting to a string just to parse back into a DateTime again.
– Sev
Nov 17 '16 at 22:22
add a comment |
Using ToString("HH:mm")
certainly gives you what you want as a string.
If you want the current hour/minute as numbers, string manipulation isn't necessary; you can use the TimeOfDay
property:
TimeSpan timeOfDay = fechaHora.TimeOfDay;
int hour = timeOfDay.Hours;
int minute = timeOfDay.Minutes;
I like this method because it gives me integer values for calculation.
– Hao Nguyen
Jan 4 '16 at 20:03
This was the preferred solution for me because I just needed the int from the hour of the day. Much easier then converting to a string just to parse back into a DateTime again.
– Sev
Nov 17 '16 at 22:22
add a comment |
Using ToString("HH:mm")
certainly gives you what you want as a string.
If you want the current hour/minute as numbers, string manipulation isn't necessary; you can use the TimeOfDay
property:
TimeSpan timeOfDay = fechaHora.TimeOfDay;
int hour = timeOfDay.Hours;
int minute = timeOfDay.Minutes;
Using ToString("HH:mm")
certainly gives you what you want as a string.
If you want the current hour/minute as numbers, string manipulation isn't necessary; you can use the TimeOfDay
property:
TimeSpan timeOfDay = fechaHora.TimeOfDay;
int hour = timeOfDay.Hours;
int minute = timeOfDay.Minutes;
answered Jun 11 '10 at 14:44
Dan Tao
94k40249401
94k40249401
I like this method because it gives me integer values for calculation.
– Hao Nguyen
Jan 4 '16 at 20:03
This was the preferred solution for me because I just needed the int from the hour of the day. Much easier then converting to a string just to parse back into a DateTime again.
– Sev
Nov 17 '16 at 22:22
add a comment |
I like this method because it gives me integer values for calculation.
– Hao Nguyen
Jan 4 '16 at 20:03
This was the preferred solution for me because I just needed the int from the hour of the day. Much easier then converting to a string just to parse back into a DateTime again.
– Sev
Nov 17 '16 at 22:22
I like this method because it gives me integer values for calculation.
– Hao Nguyen
Jan 4 '16 at 20:03
I like this method because it gives me integer values for calculation.
– Hao Nguyen
Jan 4 '16 at 20:03
This was the preferred solution for me because I just needed the int from the hour of the day. Much easier then converting to a string just to parse back into a DateTime again.
– Sev
Nov 17 '16 at 22:22
This was the preferred solution for me because I just needed the int from the hour of the day. Much easier then converting to a string just to parse back into a DateTime again.
– Sev
Nov 17 '16 at 22:22
add a comment |
Try this:
//String.Format("{0:HH:mm}", dt); // where dt is a DateTime variable
public static string FormatearHoraA24(DateTime? fechaHora)
{
if (!fechaHora.HasValue)
return "";
return retornar = String.Format("{0:HH:mm}", (DateTime)fechaHora);
}
add a comment |
Try this:
//String.Format("{0:HH:mm}", dt); // where dt is a DateTime variable
public static string FormatearHoraA24(DateTime? fechaHora)
{
if (!fechaHora.HasValue)
return "";
return retornar = String.Format("{0:HH:mm}", (DateTime)fechaHora);
}
add a comment |
Try this:
//String.Format("{0:HH:mm}", dt); // where dt is a DateTime variable
public static string FormatearHoraA24(DateTime? fechaHora)
{
if (!fechaHora.HasValue)
return "";
return retornar = String.Format("{0:HH:mm}", (DateTime)fechaHora);
}
Try this:
//String.Format("{0:HH:mm}", dt); // where dt is a DateTime variable
public static string FormatearHoraA24(DateTime? fechaHora)
{
if (!fechaHora.HasValue)
return "";
return retornar = String.Format("{0:HH:mm}", (DateTime)fechaHora);
}
edited Jun 11 '10 at 14:51
answered Jun 11 '10 at 14:39
Mike Dinescu
38.4k881121
38.4k881121
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3023649%2fhour-from-datetime-in-24-hours-format%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
@OrelEraki are you serious? This question was posted before that one by two years. At least check your facts before calling duplicate.
– Archibald
Sep 26 '16 at 5:50
There is no need to get exited, it was on 2014. Removed it.
– Orel Eraki
Sep 26 '16 at 7:17
2
"hh" in lower case is for am/pm notation, "HH" is for 24h notation
– Antoine Meltzheim
Sep 13 '17 at 13:48