Parse timestamp to get all values [duplicate] - c#

This question already has answers here:
Creating DateTime object from string
(4 answers)
Closed 7 years ago.
I have a lot of data with timestamps in the following format: 20150603005845. I need to parse this and turn it into 2015/06/03 00:58:45 AM. I know I can convert it into a string and break it up and parse it the hard way but I was wondering if there are any inbuilt classes that can help me achieve this. I'm new to C# and don't know if there are any methods that can help me do this. Any help will be appreciated!

Parse date:
var date = DateTime.ParseExact("20150603005845", "yyyyMMddHHmmss", CultureInfo.InvariantCulture);
Format date:
string formattedDate = date.ToString("yyyy/MM/dd HH:mm:ss tt");

Related

How to format string "1441/10/15" exactly same to DateTime C#? [duplicate]

This question already has answers here:
Format date in C#
(7 answers)
Closed 2 years ago.
I have string date in this format "1441/10/15" i want to change it to exact same format "1441/10/15" in datatime.
i tried to change it like this,
var yesy = Convert.ToDateTime(emp.datepicker);
but it is changing it but format is different that is "{15/10/41 12:00:00 ص}"
I need exact same format that is "1441/10/15"
thanks for your suggestion
EDITED:
I have string date "1441/10/15" how can i save it as it is in sql data base as datetime.
DateTime dt = DateTime.Parse(emp.datepicker);
string yesy = dt.ToString("yyyy/MM/dd");
Console.WriteLine(yesy);
Output = "1441/10/15"
I presume that "{15/10/41 12:00:00 ص}" is the output of yesy.ToString() in the code you didn't show to us.
Try yesy.ToString("yyyy'/'MM'/'dd").
For more information about possible output format, the doc is here as Bagus Tesa commented.

Convert string in to DateTime [duplicate]

This question already has answers here:
Converting a String to DateTime
(17 answers)
Parse a string containing date and time in a custom format
(3 answers)
Closed 4 years ago.
I have a string: "20180830" which represents 30 august 2018
I want to go to string: "30/08/2018"
So that I can do: DateTime parsedDate = DateTime.Parse("30/08/2018"); and have a DateTime instead of a string,
Tried everything but didn't succeed.
Needs some help.
You can use the DateTime.ParseExact-Method to solve your problem. Therefore you need to specify the exact format which would be yyyyMMdd in your case. Also the documentation suggests to use CultureInfo.InvariantCulture.
The following code...
DateTime datetime = DateTime.ParseExact("20180830", "yyyyMMdd", CultureInfo.InvariantCulture);
...should do the trick ;-)

C# How to convert date 25APR18 to 2018-04-26? [duplicate]

This question already has answers here:
.net: How to parse a date with this format: 08 Feb 2011 06:46
(3 answers)
Closed 4 years ago.
That question may be already been answered here, but I could not find any answers.
I'm trying to to convert the string 25APR18 to 2018-04-25.
I tried to do the following:
DateTime.ParseExact("25APR18", "yyyy-mm-dd", CultureInfo.InvariantCulture);
But it does not convert and gives me the error saying "String was not recognized as a valid DateTime."
I was not able to find the right solution.
Any suggestions?
You can use:
DateTime date = DateTime.ParseExact("25APR18", "ddMMMyy", CultureInfo.InvariantCulture);
The string format is case sensitive representing the input format you want to match. Lower case mm represents minutes but you need month abbreviated name MMM and that's one issue with the code you posted. Full list of format specifiers can be found here.
Once you have a DateTime object you can retrieve the date as string with new format:
string formattedDate = date.ToString("yyyy-MM-dd");
Which produces:
2018-04-25
Your date parse code should be
DateTime.ParseExact("25APR18", "ddMMMyy", CultureInfo.InvariantCulture);

Cannot translate date in current locale [duplicate]

This question already has answers here:
String was not recognized as a valid DateTime " format dd/MM/yyyy"
(13 answers)
Closed 5 years ago.
I'm trying to translate a Date in the current system language, this is my date: 2018/01/01, I tried to achieve my goal in this way:
var date = DateTime.ParseExact("2018/01/01", "dddd MMMM yyyy", new CultureInfo("it-IT")).ToString();
unfortunately I get:
System.ArgumentNullException
The InnerException say:
String not recognized as a valid DateTime value.
I used ParseExact to avoid this error, what I did wrong?
Use this code it will work.
var date = DateTime.ParseExact("2018/01/01", "yyyy/MM/dd", new CultureInfo("it-IT"));
I think you now know the error reason.
Thanks

DateTime in following format: 2016-01-13T11:11:11Z [duplicate]

This question already has answers here:
How can I format DateTime to web UTC format?
(9 answers)
Closed 7 years ago.
I'm trying to convert my DateTime to the following format:
ex.:
2016-01-13T11:11:11Z
But ToString("u") gives 2016-01-13 11:11:11Z
I need the letter T also between date and time. Is there a way to accomplish this?
You can supply the T directly in the format like this:
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-ddThh:mm:ssZ"));
This will give you the T and Z you are looking for.
You can replace the space with a T:
date.ToString("u").Replace(' ','T');

Categories

Resources