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;
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 aDateTime
from anotherDateTime
. This will result in aTimeSpan
object. That type has aSeconds
property. 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
667
667
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 aDateTime
from anotherDateTime
. This will result in aTimeSpan
object. That type has aSeconds
property. 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 aDateTime
from anotherDateTime
. This will result in aTimeSpan
object. That type has aSeconds
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
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.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 ofdateTime
with 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.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 ofdateTime
with 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.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 ofdateTime
with 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
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 ofdateTime
with 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.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 ofdateTime
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
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
DateTime
from anotherDateTime
. This will result in aTimeSpan
object. That type has aSeconds
property. It would simplify your calculation– Flydog57
Mar 9 at 5:18