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

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page