0

Hi I am working C# MVC project. I have got date like this

string datetime = frmcollection["txtTo"].ToString()

Here datetime variable contains date and time in following format : 06/05/2014 10:25:39

Now I need to above datetime in int, so i replaced all /, :, and space.

So now i have following result :

 int datetime = 0;
 datetime = intdatetime

so here datetime variable has following reuslts : 6052014102539

So what I need here is, I need to store int time in different format like this : 2014060514102539. so basically i need to rearrange position of inttime.

How can i do this ??

Ajay
  • 317
  • 2
  • 12
  • 25
  • If you can convert the string to a DateTime, you can format it any way you like: [stackoverflow.com/questions/3025361/c-sharp-datetime-to-yyyymmddhhmmss-format](http://stackoverflow.com/questions/3025361/c-sharp-datetime-to-yyyymmddhhmmss-format) – markpsmith Jun 05 '14 at 10:22
  • How do you assign `6052014102539` to and integer variable? – AliRıza Adıyahşi Jun 05 '14 at 10:23
  • @AliRızaAdıyahşi I am using Convert.ToInt64 – Ajay Jun 05 '14 at 10:26

4 Answers4

1
string datetime = frmcollection["txtTo"].ToString();

// your date format that is coming from form collection...
string yourDateFormat = "MM-dd-yyyy HH:mm:ss";

// convert string to date time
DateTime newDate = DateTime.ParseExact(datetime, yourDateFormat, null);

// change its format and convert it to string
string newDateStr = newDate.ToString("yyyy/MM/dd HH:mm:ss");

and use your "string to int" method...

AliRıza Adıyahşi
  • 15,658
  • 24
  • 115
  • 197
0

Create a method that takes your string, parses it to a date and returns a weird datetime int. Something like this:

public int ParseDateToWeirdInt(string date)
{
    //Error checking omitted
    var d = DateTime.Parse(date);
    var stringThatWillBecomeAnInt = "";
    stringThatWillBecomeAnInt = d.Year.ToString();
    stringThatWillBecomeAnInt += d.Month.ToString();
    stringThatWillBecomeAnInt += d.Date.ToString();
    stringThatWillBecomeAnInt += d.TimeOfDay.Hours.ToString();
    stringThatWillBecomeAnInt += d.TimeOfDay.Minutes.ToString();
    stringThatWillBecomeAnInt += d.TimeOfDay.Seconds.ToString();

    return int.Parse(stringThatWillBecomeAnInt);
}

You should probably use a StringBuilder instead of concatenating the string and a recommendation would be to turn the method into an extension method. Also note that the method needs much better error handling (the date parse could fail, the int parse could fail etc.).

Robban
  • 6,729
  • 2
  • 39
  • 47
  • if you can parse date to int with its format, this method will be perfect. I mean yours and mine combination :) some thing like this: `public int ParseDateToWeirdInt(string date, string currentFormat, string targetFormat)` – AliRıza Adıyahşi Jun 05 '14 at 10:43
0

Thanks guys for helping me out. I found this solution as easy for me .

 datetime = Convert.ToDateTime(datetime ).ToString("yyyy/dd/MM HH:mm:ss");

so now i have datetime in format i need. Now i can convert to int.

Ajay
  • 317
  • 2
  • 12
  • 25
  • You should really Parse the date and not just use Convert. Or better still use DateTime.TryParse as the date is coming from a textbox. Convert will throw an exception if it's passed in a date that has an invalid format, DateTime.TryParse will not. I never use any of the Convert methods for this exact reason. – matt_lethargic Jun 05 '14 at 13:01
0

Just adding this answer as a cleaner solution

string datetime = frmcollection["txtTo"].ToString();
string newDateTime;

DateTime theDateTime;
if (DateTime.TryParse(datetime, out theDateTime))
{
    newDateTime = theDateTime.ToString("yyyy/dd/MM HH:mm:ss");
}
else
{
    // tell user they have entered the date in wrong format
}
matt_lethargic
  • 2,706
  • 1
  • 18
  • 33