Convert Date as given in formation string Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!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
How would it unbalance gameplay to rule that Weapon Master allows for picking a fighting style?
Co-worker works way more than he should
My admission is revoked after accepting the admission offer
In search of the origins of term censor, I hit a dead end stuck with the greek term, to censor, λογοκρίνω
Not within Jobscope - Aggravated injury
Is there an efficient way for synchronising audio events real-time with LEDs using an MCU?
Are there existing rules/lore for MTG planeswalkers?
Does Prince Arnaud cause someone holding the Princess to lose?
Preserving file and folder permissions with rsync
What's parked in Mil Moscow helicopter plant?
How did Elite on the NES work?
What *exactly* is electrical current, voltage, and resistance?
Does using the Inspiration rules for character defects encourage My Guy Syndrome?
TV series episode where humans nuke aliens before decrypting their message that states they come in peace
Why doesn't the university give past final exams' answers?
Can gravitational waves pass through a black hole?
Can't solve system of linear equations (that need simplification first)
How was Lagrange appointed professor of mathematics so early?
Show two Lagrangians are equivalent
VBA: Single line if statement with multiple actions
What helicopter has the most rotor blades?
Raising a bilingual kid. When should we introduce the majority language?
When I export an AI 300x60 art board it saves with bigger dimensions
Why do people think Winterfell crypts is the safest place for women, children & old people?
Convert Date as given in formation string
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!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;
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
add a comment |
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
3
Have you looked atDatetime.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 aDateTimefrom anotherDateTime. This will result in aTimeSpanobject. That type has aSecondsproperty. It would simplify your calculation
– Flydog57
Mar 9 at 5:18
add a comment |
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
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
c# date
asked Mar 9 at 4:13
Tanveer HasanTanveer Hasan
767
767
3
Have you looked atDatetime.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 aDateTimefrom anotherDateTime. This will result in aTimeSpanobject. That type has aSecondsproperty. It would simplify your calculation
– Flydog57
Mar 9 at 5:18
add a comment |
3
Have you looked atDatetime.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 aDateTimefrom anotherDateTime. This will result in aTimeSpanobject. That type has aSecondsproperty. 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
add a comment |
1 Answer
1
active
oldest
votes
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);
1
To extract the year, month, and day, you can use the relevant properties:int year = dateTime.Yearint month = dateTime.Monthint day = dateTime.DayThis avoids creating strings just to read them back into ints again. To get just the date component you can use:var toDate = dateTime.Datewhich returns a copy ofdateTimewith all the time information stripped (set to zero).
– James
Mar 9 at 5:02
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%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
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);
1
To extract the year, month, and day, you can use the relevant properties:int year = dateTime.Yearint month = dateTime.Monthint day = dateTime.DayThis avoids creating strings just to read them back into ints again. To get just the date component you can use:var toDate = dateTime.Datewhich returns a copy ofdateTimewith all the time information stripped (set to zero).
– James
Mar 9 at 5:02
add a comment |
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);
1
To extract the year, month, and day, you can use the relevant properties:int year = dateTime.Yearint month = dateTime.Monthint day = dateTime.DayThis avoids creating strings just to read them back into ints again. To get just the date component you can use:var toDate = dateTime.Datewhich returns a copy ofdateTimewith all the time information stripped (set to zero).
– James
Mar 9 at 5:02
add a comment |
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);
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);
edited Mar 9 at 5:19
answered Mar 9 at 4:51
Tanveer HasanTanveer Hasan
767
767
1
To extract the year, month, and day, you can use the relevant properties:int year = dateTime.Yearint month = dateTime.Monthint day = dateTime.DayThis avoids creating strings just to read them back into ints again. To get just the date component you can use:var toDate = dateTime.Datewhich returns a copy ofdateTimewith all the time information stripped (set to zero).
– James
Mar 9 at 5:02
add a comment |
1
To extract the year, month, and day, you can use the relevant properties:int year = dateTime.Yearint month = dateTime.Monthint day = dateTime.DayThis avoids creating strings just to read them back into ints again. To get just the date component you can use:var toDate = dateTime.Datewhich returns a copy ofdateTimewith 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
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.
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%2f55073913%2fconvert-date-as-given-in-formation-string%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
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
DateTimefrom anotherDateTime. This will result in aTimeSpanobject. That type has aSecondsproperty. It would simplify your calculation– Flydog57
Mar 9 at 5:18