Net C# Date Time conversion












1















I would like to convert date as below:



"Friday, November 2 2018 6:12 PM"



To



"20181102|18:12"



Appreciate it.



My coding as below, and show string was not recognized as valid datetime. Anyone can help me out?



String DateCv_01 = dtB.Rows[i_01]["APi_Time"].ToString();

DateTime DateCv_02 = DateTime.ParseExact(DateCv_01.ToString(), "dddd, MMMM dd yyyy hh:mm tt", CultureInfo.InvariantCulture);

string DateCv_03 = DateCv_02.ToString("yyyyMMdd|hh:mm");









share|improve this question




















  • 2





    Welcome to Stack Overflow. You should use DateTime.ParseExact to convert the string to a DateTime. Then use ToString with the appropriate format string to convert it back to a string in the desired format. I suggest you read docs.microsoft.com/en-us/dotnet/standard/base-types/…, try to implement it yourself, and then edit your question with what you've tried and what went wrong if you get stuck.

    – Jon Skeet
    Nov 15 '18 at 10:26






  • 2





    Okay, your question is getting better - but "but error" doesn't give us enough information to help you.

    – Jon Skeet
    Nov 15 '18 at 10:30






  • 4





    I realise we're all trying to be nice now, but how does this have 3 upvotes?

    – DavidG
    Nov 15 '18 at 10:32






  • 2





    You are almost done it. Try to use only on d and h in your format. dddd, MMMM d yyyy h:mm tt

    – Reniuz
    Nov 15 '18 at 10:36






  • 2





    What is the real type of dtB.Rows[i_01]["APi_Time"], before you convert it to string? If it already is a DateTime, then cast to a DateTime

    – Hans Kesting
    Nov 15 '18 at 10:37
















1















I would like to convert date as below:



"Friday, November 2 2018 6:12 PM"



To



"20181102|18:12"



Appreciate it.



My coding as below, and show string was not recognized as valid datetime. Anyone can help me out?



String DateCv_01 = dtB.Rows[i_01]["APi_Time"].ToString();

DateTime DateCv_02 = DateTime.ParseExact(DateCv_01.ToString(), "dddd, MMMM dd yyyy hh:mm tt", CultureInfo.InvariantCulture);

string DateCv_03 = DateCv_02.ToString("yyyyMMdd|hh:mm");









share|improve this question




















  • 2





    Welcome to Stack Overflow. You should use DateTime.ParseExact to convert the string to a DateTime. Then use ToString with the appropriate format string to convert it back to a string in the desired format. I suggest you read docs.microsoft.com/en-us/dotnet/standard/base-types/…, try to implement it yourself, and then edit your question with what you've tried and what went wrong if you get stuck.

    – Jon Skeet
    Nov 15 '18 at 10:26






  • 2





    Okay, your question is getting better - but "but error" doesn't give us enough information to help you.

    – Jon Skeet
    Nov 15 '18 at 10:30






  • 4





    I realise we're all trying to be nice now, but how does this have 3 upvotes?

    – DavidG
    Nov 15 '18 at 10:32






  • 2





    You are almost done it. Try to use only on d and h in your format. dddd, MMMM d yyyy h:mm tt

    – Reniuz
    Nov 15 '18 at 10:36






  • 2





    What is the real type of dtB.Rows[i_01]["APi_Time"], before you convert it to string? If it already is a DateTime, then cast to a DateTime

    – Hans Kesting
    Nov 15 '18 at 10:37














1












1








1








I would like to convert date as below:



"Friday, November 2 2018 6:12 PM"



To



"20181102|18:12"



Appreciate it.



My coding as below, and show string was not recognized as valid datetime. Anyone can help me out?



String DateCv_01 = dtB.Rows[i_01]["APi_Time"].ToString();

DateTime DateCv_02 = DateTime.ParseExact(DateCv_01.ToString(), "dddd, MMMM dd yyyy hh:mm tt", CultureInfo.InvariantCulture);

string DateCv_03 = DateCv_02.ToString("yyyyMMdd|hh:mm");









share|improve this question
















I would like to convert date as below:



"Friday, November 2 2018 6:12 PM"



To



"20181102|18:12"



Appreciate it.



My coding as below, and show string was not recognized as valid datetime. Anyone can help me out?



String DateCv_01 = dtB.Rows[i_01]["APi_Time"].ToString();

DateTime DateCv_02 = DateTime.ParseExact(DateCv_01.ToString(), "dddd, MMMM dd yyyy hh:mm tt", CultureInfo.InvariantCulture);

string DateCv_03 = DateCv_02.ToString("yyyyMMdd|hh:mm");






c#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 10:34







Kenneth Tan

















asked Nov 15 '18 at 10:25









Kenneth TanKenneth Tan

213




213








  • 2





    Welcome to Stack Overflow. You should use DateTime.ParseExact to convert the string to a DateTime. Then use ToString with the appropriate format string to convert it back to a string in the desired format. I suggest you read docs.microsoft.com/en-us/dotnet/standard/base-types/…, try to implement it yourself, and then edit your question with what you've tried and what went wrong if you get stuck.

    – Jon Skeet
    Nov 15 '18 at 10:26






  • 2





    Okay, your question is getting better - but "but error" doesn't give us enough information to help you.

    – Jon Skeet
    Nov 15 '18 at 10:30






  • 4





    I realise we're all trying to be nice now, but how does this have 3 upvotes?

    – DavidG
    Nov 15 '18 at 10:32






  • 2





    You are almost done it. Try to use only on d and h in your format. dddd, MMMM d yyyy h:mm tt

    – Reniuz
    Nov 15 '18 at 10:36






  • 2





    What is the real type of dtB.Rows[i_01]["APi_Time"], before you convert it to string? If it already is a DateTime, then cast to a DateTime

    – Hans Kesting
    Nov 15 '18 at 10:37














  • 2





    Welcome to Stack Overflow. You should use DateTime.ParseExact to convert the string to a DateTime. Then use ToString with the appropriate format string to convert it back to a string in the desired format. I suggest you read docs.microsoft.com/en-us/dotnet/standard/base-types/…, try to implement it yourself, and then edit your question with what you've tried and what went wrong if you get stuck.

    – Jon Skeet
    Nov 15 '18 at 10:26






  • 2





    Okay, your question is getting better - but "but error" doesn't give us enough information to help you.

    – Jon Skeet
    Nov 15 '18 at 10:30






  • 4





    I realise we're all trying to be nice now, but how does this have 3 upvotes?

    – DavidG
    Nov 15 '18 at 10:32






  • 2





    You are almost done it. Try to use only on d and h in your format. dddd, MMMM d yyyy h:mm tt

    – Reniuz
    Nov 15 '18 at 10:36






  • 2





    What is the real type of dtB.Rows[i_01]["APi_Time"], before you convert it to string? If it already is a DateTime, then cast to a DateTime

    – Hans Kesting
    Nov 15 '18 at 10:37








2




2





Welcome to Stack Overflow. You should use DateTime.ParseExact to convert the string to a DateTime. Then use ToString with the appropriate format string to convert it back to a string in the desired format. I suggest you read docs.microsoft.com/en-us/dotnet/standard/base-types/…, try to implement it yourself, and then edit your question with what you've tried and what went wrong if you get stuck.

– Jon Skeet
Nov 15 '18 at 10:26





Welcome to Stack Overflow. You should use DateTime.ParseExact to convert the string to a DateTime. Then use ToString with the appropriate format string to convert it back to a string in the desired format. I suggest you read docs.microsoft.com/en-us/dotnet/standard/base-types/…, try to implement it yourself, and then edit your question with what you've tried and what went wrong if you get stuck.

– Jon Skeet
Nov 15 '18 at 10:26




2




2





Okay, your question is getting better - but "but error" doesn't give us enough information to help you.

– Jon Skeet
Nov 15 '18 at 10:30





Okay, your question is getting better - but "but error" doesn't give us enough information to help you.

– Jon Skeet
Nov 15 '18 at 10:30




4




4





I realise we're all trying to be nice now, but how does this have 3 upvotes?

– DavidG
Nov 15 '18 at 10:32





I realise we're all trying to be nice now, but how does this have 3 upvotes?

– DavidG
Nov 15 '18 at 10:32




2




2





You are almost done it. Try to use only on d and h in your format. dddd, MMMM d yyyy h:mm tt

– Reniuz
Nov 15 '18 at 10:36





You are almost done it. Try to use only on d and h in your format. dddd, MMMM d yyyy h:mm tt

– Reniuz
Nov 15 '18 at 10:36




2




2





What is the real type of dtB.Rows[i_01]["APi_Time"], before you convert it to string? If it already is a DateTime, then cast to a DateTime

– Hans Kesting
Nov 15 '18 at 10:37





What is the real type of dtB.Rows[i_01]["APi_Time"], before you convert it to string? If it already is a DateTime, then cast to a DateTime

– Hans Kesting
Nov 15 '18 at 10:37












1 Answer
1






active

oldest

votes


















1














Following is the usage of days and hours:



enter image description here



Which means following datetime format should be used:



dddd, MMMM d yyyy h:mm tt



Try below code:



DateTime DateCv_02 = DateTime.ParseExact("Friday, November 2 2018 6:12 PM", "dddd, MMMM d yyyy h:mm tt", CultureInfo.InvariantCulture);

string DateCv_03 = DateCv_02.ToString("yyyyMMdd|hh:mm");





share|improve this answer
























  • How you answered to a closed question?

    – SeM
    Nov 15 '18 at 10:53











  • @SeM it is simple.. I am Batman!

    – FaizanRabbani
    Nov 15 '18 at 10:57








  • 1





    @FaizanRabbani hm, ok then :)

    – SeM
    Nov 15 '18 at 12:36






  • 1





    @ADyson When I asked, he have answered about 3 min ago, but question was put on hold about 10-15 minutes ago, so it was kind of strange (or maybe I didn't noticed that question has an answer, but I was pretty sure it doesn't) :)

    – SeM
    Nov 15 '18 at 12:37








  • 1





    Thank you so much, @FaizanRabbani. It's work for me

    – Kenneth Tan
    Nov 16 '18 at 0:49











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53317278%2fnet-c-sharp-date-time-conversion%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Following is the usage of days and hours:



enter image description here



Which means following datetime format should be used:



dddd, MMMM d yyyy h:mm tt



Try below code:



DateTime DateCv_02 = DateTime.ParseExact("Friday, November 2 2018 6:12 PM", "dddd, MMMM d yyyy h:mm tt", CultureInfo.InvariantCulture);

string DateCv_03 = DateCv_02.ToString("yyyyMMdd|hh:mm");





share|improve this answer
























  • How you answered to a closed question?

    – SeM
    Nov 15 '18 at 10:53











  • @SeM it is simple.. I am Batman!

    – FaizanRabbani
    Nov 15 '18 at 10:57








  • 1





    @FaizanRabbani hm, ok then :)

    – SeM
    Nov 15 '18 at 12:36






  • 1





    @ADyson When I asked, he have answered about 3 min ago, but question was put on hold about 10-15 minutes ago, so it was kind of strange (or maybe I didn't noticed that question has an answer, but I was pretty sure it doesn't) :)

    – SeM
    Nov 15 '18 at 12:37








  • 1





    Thank you so much, @FaizanRabbani. It's work for me

    – Kenneth Tan
    Nov 16 '18 at 0:49
















1














Following is the usage of days and hours:



enter image description here



Which means following datetime format should be used:



dddd, MMMM d yyyy h:mm tt



Try below code:



DateTime DateCv_02 = DateTime.ParseExact("Friday, November 2 2018 6:12 PM", "dddd, MMMM d yyyy h:mm tt", CultureInfo.InvariantCulture);

string DateCv_03 = DateCv_02.ToString("yyyyMMdd|hh:mm");





share|improve this answer
























  • How you answered to a closed question?

    – SeM
    Nov 15 '18 at 10:53











  • @SeM it is simple.. I am Batman!

    – FaizanRabbani
    Nov 15 '18 at 10:57








  • 1





    @FaizanRabbani hm, ok then :)

    – SeM
    Nov 15 '18 at 12:36






  • 1





    @ADyson When I asked, he have answered about 3 min ago, but question was put on hold about 10-15 minutes ago, so it was kind of strange (or maybe I didn't noticed that question has an answer, but I was pretty sure it doesn't) :)

    – SeM
    Nov 15 '18 at 12:37








  • 1





    Thank you so much, @FaizanRabbani. It's work for me

    – Kenneth Tan
    Nov 16 '18 at 0:49














1












1








1







Following is the usage of days and hours:



enter image description here



Which means following datetime format should be used:



dddd, MMMM d yyyy h:mm tt



Try below code:



DateTime DateCv_02 = DateTime.ParseExact("Friday, November 2 2018 6:12 PM", "dddd, MMMM d yyyy h:mm tt", CultureInfo.InvariantCulture);

string DateCv_03 = DateCv_02.ToString("yyyyMMdd|hh:mm");





share|improve this answer













Following is the usage of days and hours:



enter image description here



Which means following datetime format should be used:



dddd, MMMM d yyyy h:mm tt



Try below code:



DateTime DateCv_02 = DateTime.ParseExact("Friday, November 2 2018 6:12 PM", "dddd, MMMM d yyyy h:mm tt", CultureInfo.InvariantCulture);

string DateCv_03 = DateCv_02.ToString("yyyyMMdd|hh:mm");






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 10:48









FaizanRabbaniFaizanRabbani

2,22811432




2,22811432













  • How you answered to a closed question?

    – SeM
    Nov 15 '18 at 10:53











  • @SeM it is simple.. I am Batman!

    – FaizanRabbani
    Nov 15 '18 at 10:57








  • 1





    @FaizanRabbani hm, ok then :)

    – SeM
    Nov 15 '18 at 12:36






  • 1





    @ADyson When I asked, he have answered about 3 min ago, but question was put on hold about 10-15 minutes ago, so it was kind of strange (or maybe I didn't noticed that question has an answer, but I was pretty sure it doesn't) :)

    – SeM
    Nov 15 '18 at 12:37








  • 1





    Thank you so much, @FaizanRabbani. It's work for me

    – Kenneth Tan
    Nov 16 '18 at 0:49



















  • How you answered to a closed question?

    – SeM
    Nov 15 '18 at 10:53











  • @SeM it is simple.. I am Batman!

    – FaizanRabbani
    Nov 15 '18 at 10:57








  • 1





    @FaizanRabbani hm, ok then :)

    – SeM
    Nov 15 '18 at 12:36






  • 1





    @ADyson When I asked, he have answered about 3 min ago, but question was put on hold about 10-15 minutes ago, so it was kind of strange (or maybe I didn't noticed that question has an answer, but I was pretty sure it doesn't) :)

    – SeM
    Nov 15 '18 at 12:37








  • 1





    Thank you so much, @FaizanRabbani. It's work for me

    – Kenneth Tan
    Nov 16 '18 at 0:49

















How you answered to a closed question?

– SeM
Nov 15 '18 at 10:53





How you answered to a closed question?

– SeM
Nov 15 '18 at 10:53













@SeM it is simple.. I am Batman!

– FaizanRabbani
Nov 15 '18 at 10:57







@SeM it is simple.. I am Batman!

– FaizanRabbani
Nov 15 '18 at 10:57






1




1





@FaizanRabbani hm, ok then :)

– SeM
Nov 15 '18 at 12:36





@FaizanRabbani hm, ok then :)

– SeM
Nov 15 '18 at 12:36




1




1





@ADyson When I asked, he have answered about 3 min ago, but question was put on hold about 10-15 minutes ago, so it was kind of strange (or maybe I didn't noticed that question has an answer, but I was pretty sure it doesn't) :)

– SeM
Nov 15 '18 at 12:37







@ADyson When I asked, he have answered about 3 min ago, but question was put on hold about 10-15 minutes ago, so it was kind of strange (or maybe I didn't noticed that question has an answer, but I was pretty sure it doesn't) :)

– SeM
Nov 15 '18 at 12:37






1




1





Thank you so much, @FaizanRabbani. It's work for me

– Kenneth Tan
Nov 16 '18 at 0:49





Thank you so much, @FaizanRabbani. It's work for me

– Kenneth Tan
Nov 16 '18 at 0:49




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53317278%2fnet-c-sharp-date-time-conversion%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Bressuire

Vorschmack

Quarantine