Convert Date as given in formation stringWhat is the difference between String and string in C#?How to return only the Date from a SQL Server DateTime datatypeCase insensitive 'Contains(string)'Compare two dates with JavaScriptWhere can I find documentation on formatting a date in JavaScript?Detecting an “invalid date” Date instance in JavaScriptYYYY-MM-DD format date in shell scriptHow do I get the current date in JavaScript?Calculate difference between two dates (number of days)?How to format a JavaScript date

Example of a relative pronoun

Why is "Reports" in sentence down without "The"

Divisibility of sum of multinomials

Why did the Germans forbid the possession of pet pigeons in Rostov-on-Don in 1941?

Finding files for which a command fails

Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?

How to register event with useEffect hooks?

A Journey Through Space and Time

How can bays and straits be determined in a procedurally generated map?

Why is the design of haulage companies so “special”?

Circuitry of TV splitters

Are tax years 2016 & 2017 back taxes deductible for tax year 2018?

Download, install and reboot computer at night if needed

Is there a minimum number of transactions in a block?

Do airline pilots ever risk not hearing communication directed to them specifically, from traffic controllers?

Schwarzchild Radius of the Universe

N.B. ligature in Latex

Infinite past with a beginning?

Should I join an office cleaning event for free?

Can town administrative "code" overule state laws like those forbidding trespassing?

Where to refill my bottle in India?

"listening to me about as much as you're listening to this pole here"

"The augmented fourth (A4) and the diminished fifth (d5) are the only augmented and diminished intervals that appear in diatonic scales"

Why do we use polarized capacitor?



Convert Date as given in formation string


What is the difference between String and string in C#?How to return only the Date from a SQL Server DateTime datatypeCase insensitive 'Contains(string)'Compare two dates with JavaScriptWhere can I find documentation on formatting a date in JavaScript?Detecting an “invalid date” Date instance in JavaScriptYYYY-MM-DD format date in shell scriptHow do I get the current date in JavaScript?Calculate difference between two dates (number of days)?How to format a JavaScript date






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I have a function that converts a given date to timestamp. the date format is dynamic. for example (it could be 'dd/MM/yyyy' or 'dd-MM-yyyy' or MM/dd/yyyy).but date format is also passed as an argument in the function. i need to seperate day , month and year for this conversion. how can i separate as given in formation string



 public static double GetTimeStamp(string date, string format)

string[] dateToConvert = date.Split('/');

int year=Int32.Parse(dateToConvert[2]);
int month=Int32.Parse(dateToConvert[1]);
int day=Int32.Parse(dateToConvert[0]);

var baseDate = new DateTime(1970, 01, 01);
var toDate = new DateTime(year, month, day);
var numberOfSeconds = toDate.Subtract(baseDate).TotalSeconds;
return numberOfSeconds;



i am using '/' as a separation character. but i want to separate it as provided in the formation. if formation string is (dd-MM-yyyy). i need to seperate it using '-' charecter










share|improve this question

















  • 3





    Have you looked at Datetime.ParseExact? docs.microsoft.com/en-us/dotnet/api/system.datetime.parseexact

    – Flydog57
    Mar 9 at 4:19












  • Yea It works for me. thaks

    – Tanveer Hasan
    Mar 9 at 4:49











  • One other note: you can subtract a DateTime from another DateTime. This will result in a TimeSpan object. That type has a Seconds property. It would simplify your calculation

    – Flydog57
    Mar 9 at 5:18

















1















I have a function that converts a given date to timestamp. the date format is dynamic. for example (it could be 'dd/MM/yyyy' or 'dd-MM-yyyy' or MM/dd/yyyy).but date format is also passed as an argument in the function. i need to seperate day , month and year for this conversion. how can i separate as given in formation string



 public static double GetTimeStamp(string date, string format)

string[] dateToConvert = date.Split('/');

int year=Int32.Parse(dateToConvert[2]);
int month=Int32.Parse(dateToConvert[1]);
int day=Int32.Parse(dateToConvert[0]);

var baseDate = new DateTime(1970, 01, 01);
var toDate = new DateTime(year, month, day);
var numberOfSeconds = toDate.Subtract(baseDate).TotalSeconds;
return numberOfSeconds;



i am using '/' as a separation character. but i want to separate it as provided in the formation. if formation string is (dd-MM-yyyy). i need to seperate it using '-' charecter










share|improve this question

















  • 3





    Have you looked at Datetime.ParseExact? docs.microsoft.com/en-us/dotnet/api/system.datetime.parseexact

    – Flydog57
    Mar 9 at 4:19












  • Yea It works for me. thaks

    – Tanveer Hasan
    Mar 9 at 4:49











  • One other note: you can subtract a DateTime from another DateTime. This will result in a TimeSpan object. That type has a Seconds property. It would simplify your calculation

    – Flydog57
    Mar 9 at 5:18













1












1








1








I have a function that converts a given date to timestamp. the date format is dynamic. for example (it could be 'dd/MM/yyyy' or 'dd-MM-yyyy' or MM/dd/yyyy).but date format is also passed as an argument in the function. i need to seperate day , month and year for this conversion. how can i separate as given in formation string



 public static double GetTimeStamp(string date, string format)

string[] dateToConvert = date.Split('/');

int year=Int32.Parse(dateToConvert[2]);
int month=Int32.Parse(dateToConvert[1]);
int day=Int32.Parse(dateToConvert[0]);

var baseDate = new DateTime(1970, 01, 01);
var toDate = new DateTime(year, month, day);
var numberOfSeconds = toDate.Subtract(baseDate).TotalSeconds;
return numberOfSeconds;



i am using '/' as a separation character. but i want to separate it as provided in the formation. if formation string is (dd-MM-yyyy). i need to seperate it using '-' charecter










share|improve this question














I have a function that converts a given date to timestamp. the date format is dynamic. for example (it could be 'dd/MM/yyyy' or 'dd-MM-yyyy' or MM/dd/yyyy).but date format is also passed as an argument in the function. i need to seperate day , month and year for this conversion. how can i separate as given in formation string



 public static double GetTimeStamp(string date, string format)

string[] dateToConvert = date.Split('/');

int year=Int32.Parse(dateToConvert[2]);
int month=Int32.Parse(dateToConvert[1]);
int day=Int32.Parse(dateToConvert[0]);

var baseDate = new DateTime(1970, 01, 01);
var toDate = new DateTime(year, month, day);
var numberOfSeconds = toDate.Subtract(baseDate).TotalSeconds;
return numberOfSeconds;



i am using '/' as a separation character. but i want to separate it as provided in the formation. if formation string is (dd-MM-yyyy). i need to seperate it using '-' charecter







c# date






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 9 at 4:13









Tanveer HasanTanveer Hasan

667




667







  • 3





    Have you looked at Datetime.ParseExact? docs.microsoft.com/en-us/dotnet/api/system.datetime.parseexact

    – Flydog57
    Mar 9 at 4:19












  • Yea It works for me. thaks

    – Tanveer Hasan
    Mar 9 at 4:49











  • One other note: you can subtract a DateTime from another DateTime. This will result in a TimeSpan object. That type has a Seconds property. It would simplify your calculation

    – Flydog57
    Mar 9 at 5:18












  • 3





    Have you looked at Datetime.ParseExact? docs.microsoft.com/en-us/dotnet/api/system.datetime.parseexact

    – Flydog57
    Mar 9 at 4:19












  • Yea It works for me. thaks

    – Tanveer Hasan
    Mar 9 at 4:49











  • One other note: you can subtract a DateTime from another DateTime. This will result in a TimeSpan object. That type has a Seconds property. It would simplify your calculation

    – Flydog57
    Mar 9 at 5:18







3




3





Have you looked at Datetime.ParseExact? docs.microsoft.com/en-us/dotnet/api/system.datetime.parseexact

– Flydog57
Mar 9 at 4:19






Have you looked at Datetime.ParseExact? docs.microsoft.com/en-us/dotnet/api/system.datetime.parseexact

– Flydog57
Mar 9 at 4:19














Yea It works for me. thaks

– Tanveer Hasan
Mar 9 at 4:49





Yea It works for me. thaks

– Tanveer Hasan
Mar 9 at 4:49













One other note: you can subtract a DateTime from another DateTime. This will result in a TimeSpan object. That type has a Seconds property. It would simplify your calculation

– Flydog57
Mar 9 at 5:18





One other note: you can subtract a DateTime from another DateTime. This will result in a TimeSpan object. That type has a Seconds property. It would simplify your calculation

– Flydog57
Mar 9 at 5:18












1 Answer
1






active

oldest

votes


















0














 DateTime dateTime = DateTime.ParseExact(date, format, null);


int year = dateTime.Year;
int month=dateTime.Month;
int day = dateTime.Day;

var toDate = new DateTime(year, month, day);





share|improve this answer




















  • 1





    To extract the year, month, and day, you can use the relevant properties: int year = dateTime.Year int month = dateTime.Month int day = dateTime.Day This avoids creating strings just to read them back into ints again. To get just the date component you can use: var toDate = dateTime.Date which returns a copy of dateTime with all the time information stripped (set to zero).

    – James
    Mar 9 at 5:02












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%2f55073913%2fconvert-date-as-given-in-formation-string%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









0














 DateTime dateTime = DateTime.ParseExact(date, format, null);


int year = dateTime.Year;
int month=dateTime.Month;
int day = dateTime.Day;

var toDate = new DateTime(year, month, day);





share|improve this answer




















  • 1





    To extract the year, month, and day, you can use the relevant properties: int year = dateTime.Year int month = dateTime.Month int day = dateTime.Day This avoids creating strings just to read them back into ints again. To get just the date component you can use: var toDate = dateTime.Date which returns a copy of dateTime with all the time information stripped (set to zero).

    – James
    Mar 9 at 5:02
















0














 DateTime dateTime = DateTime.ParseExact(date, format, null);


int year = dateTime.Year;
int month=dateTime.Month;
int day = dateTime.Day;

var toDate = new DateTime(year, month, day);





share|improve this answer




















  • 1





    To extract the year, month, and day, you can use the relevant properties: int year = dateTime.Year int month = dateTime.Month int day = dateTime.Day This avoids creating strings just to read them back into ints again. To get just the date component you can use: var toDate = dateTime.Date which returns a copy of dateTime with all the time information stripped (set to zero).

    – James
    Mar 9 at 5:02














0












0








0







 DateTime dateTime = DateTime.ParseExact(date, format, null);


int year = dateTime.Year;
int month=dateTime.Month;
int day = dateTime.Day;

var toDate = new DateTime(year, month, day);





share|improve this answer















 DateTime dateTime = DateTime.ParseExact(date, format, null);


int year = dateTime.Year;
int month=dateTime.Month;
int day = dateTime.Day;

var toDate = new DateTime(year, month, day);






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 9 at 5:19

























answered Mar 9 at 4:51









Tanveer HasanTanveer Hasan

667




667







  • 1





    To extract the year, month, and day, you can use the relevant properties: int year = dateTime.Year int month = dateTime.Month int day = dateTime.Day This avoids creating strings just to read them back into ints again. To get just the date component you can use: var toDate = dateTime.Date which returns a copy of dateTime with all the time information stripped (set to zero).

    – James
    Mar 9 at 5:02













  • 1





    To extract the year, month, and day, you can use the relevant properties: int year = dateTime.Year int month = dateTime.Month int day = dateTime.Day This avoids creating strings just to read them back into ints again. To get just the date component you can use: var toDate = dateTime.Date which returns a copy of dateTime with all the time information stripped (set to zero).

    – James
    Mar 9 at 5:02








1




1





To extract the year, month, and day, you can use the relevant properties: int year = dateTime.Year int month = dateTime.Month int day = dateTime.Day This avoids creating strings just to read them back into ints again. To get just the date component you can use: var toDate = dateTime.Date which returns a copy of dateTime with all the time information stripped (set to zero).

– James
Mar 9 at 5:02






To extract the year, month, and day, you can use the relevant properties: int year = dateTime.Year int month = dateTime.Month int day = dateTime.Day This avoids creating strings just to read them back into ints again. To get just the date component you can use: var toDate = dateTime.Date which returns a copy of dateTime with all the time information stripped (set to zero).

– James
Mar 9 at 5:02




















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%2f55073913%2fconvert-date-as-given-in-formation-string%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

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived