How do I arrange date time in following format - asp.net-mvc

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 ??

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...

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.).

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.

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
}

Related

How to give a date Format to the sting parameter in query string? [duplicate]

I have to convert string in mm/dd/yyyy format to datetime variable but it should remain in mm/dd/yyyy format.
string strDate = DateTime.Now.ToString("MM/dd/yyyy");
Please help.
You are looking for the DateTime.Parse() method (MSDN Article)
So you can do:
var dateTime = DateTime.Parse("01/01/2001");
Which will give you a DateTime typed object.
If you need to specify which date format you want to use, you would use DateTime.ParseExact (MSDN Article)
Which you would use in a situation like this (Where you are using a British style date format):
string[] formats= { "dd/MM/yyyy" }
var dateTime = DateTime.ParseExact("01/01/2001", formats, new CultureInfo("en-US"), DateTimeStyles.None);
You need an uppercase M for the month part.
string strDate = DateTime.Now.ToString("MM/dd/yyyy");
Lowercase m is for outputting (and parsing) a minute (such as h:mm).
e.g. a full date time string might look like this:
string strDate = DateTime.Now.ToString("MM/dd/yyyy h:mm");
Notice the uppercase/lowercase mM difference.
Also if you will always deal with the same datetime format string, you can make it easier by writing them as C# extension methods.
public static class DateTimeMyFormatExtensions
{
public static string ToMyFormatString(this DateTime dt)
{
return dt.ToString("MM/dd/yyyy");
}
}
public static class StringMyDateTimeFormatExtension
{
public static DateTime ParseMyFormatDateTime(this string s)
{
var culture = System.Globalization.CultureInfo.CurrentCulture;
return DateTime.ParseExact(s, "MM/dd/yyyy", culture);
}
}
EXAMPLE: Translating between DateTime/string
DateTime now = DateTime.Now;
string strNow = now.ToMyFormatString();
DateTime nowAgain = strNow.ParseMyFormatDateTime();
Note that there is NO way to store a custom DateTime format information to use as default as in .NET most string formatting depends on the currently set culture, i.e.
System.Globalization.CultureInfo.CurrentCulture.
The only easy way you can do is to roll a custom extension method.
Also, the other easy way would be to use a different "container" or "wrapper" class for your DateTime, i.e. some special class with explicit operator defined that automatically translates to and from DateTime/string. But that is dangerous territory.
Solution
DateTime.Now.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture)
I did like this
var datetoEnter= DateTime.ParseExact(createdDate, "dd/mm/yyyy", CultureInfo.InvariantCulture);
You can change the format too by doing this
string fecha = DateTime.Now.ToString(format:"dd-MM-yyyy");
// this change the "/" for the "-"
The following works for me.
string strToday = DateTime.Today.ToString("MM/dd/yyyy");

unable to convert string date in Format yyyyMMddHHmmss to DateTime dart

i have a string containing date in format yyyyMMddHHmmss (e.g.) (20180626170555) and i am using following code to convert it into date time
dateTimeFromString(json['dateTime'], "yyyyMMddHHmmss")
exception is:
FormatException: Trying to read MM from 20180623130424 at position 14
what can be the reason?
DateTime.parse("string date here") accept some formatted string only. Check below examples of accepted strings.
"2012-02-27 13:27:00"
"2012-02-27 13:27:00.123456789z"
"2012-02-27 13:27:00,123456789z"
"20120227 13:27:00"
"20120227T132700"
"20120227"
"+20120227"
"2012-02-27T14Z"
"2012-02-27T14+00:00"
"-123450101 00:00:00 Z": in the year -12345.
"2002-02-27T14:00:00-0500": Same as "2002-02-27T19:00:00Z"
=> String to DateTime
DateTime tempDate = new DateFormat("yyyy-MM-dd hh:mm:ss").parse(savedDateString);
=> DateTime to String
String date = DateFormat("yyyy-MM-dd hh:mm:ss").format(DateTime.now());
Reference links:
Use intl for DateFormat from flutter package (https://pub.dev/packages/intl)
DateTime.parse() => https://api.dart.dev/stable/2.7.2/dart-core/DateTime/parse.html
intl DateFormat can't cope with your input string as it doesn't have any separators. The whole string gets consumed as the year. However DateTime.parse does cope with this (nearly). It happens to expect precisely the format you have (again, nearly).
One of the acceptable styles to parse is 20120227T132700, which just differs by the T date/time separator.
Try this:
String date = '20180626170555';
String dateWithT = date.substring(0, 8) + 'T' + date.substring(8);
DateTime dateTime = DateTime.parse(dateWithT);
to convert from "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" to 'MM/dd/yyyy hh:mm a'
date = '2021-01-26T03:17:00.000000Z';
DateTime parseDate =
new DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").parse(date);
var inputDate = DateTime.parse(parseDate.toString());
var outputFormat = DateFormat('MM/dd/yyyy hh:mm a');
var outputDate = outputFormat.format(inputDate);
print(outputDate)
output
01/26/2021 03:17 AM
You can use DateFormat to parse a DateTime from string to an object
// With en_US locale by default
var newDateTimeObj = new DateFormat().add_yMd().add_Hms().parse("7/10/1996 10:07:23")
// with a defined format
var newDateTimeObj2 = new DateFormat("dd/MM/yyyy HH:mm:ss").parse("10/02/2000 15:13:09")
Check the doc here.
The Easient way convert a string into Date format is
print(DateTime.parse('2020-01-02')); // 2020-01-02 00:00:00.000
print(DateTime.parse('20200102')); // 2020-01-02 00:00:00.000
print(DateTime.parse('-12345-03-04')); // -12345-03-04 00:00:00.000
print(DateTime.parse('2020-01-02 07')); // 2020-01-02 07:00:00.000
print(DateTime.parse('2020-01-02T07')); // 2020-01-02 07:00:00.000
print(DateTime.parse('2020-01-02T07:12')); // 2020-01-02 07:12:00.000
print(DateTime.parse('2020-01-02T07:12:50')); // 2020-01-02 07:12:50.000
print(DateTime.parse('2020-01-02T07:12:50Z')); // 2020-01-02 07:12:50.000Z
print(DateTime.parse('2020-01-02T07:12:50+07')); // 2020-01-02 00:12:50.000Z
print(DateTime.parse('2020-01-02T07:12:50+0700')); // 2020-01-02 00:12:50.00
print(DateTime.parse('2020-01-02T07:12:50+07:00')); // 2020-01-02 00:12:50.00
From the docs, you need Single M to month in year :
dateTimeFromString(json['dateTime'], "yMdHms")
Basic information about how to convert String to Date and Date to string in flutter. Look at below link
https://quickstartflutterdart.blogspot.com/2018/10/how-to-convert-string-to-date-and-date.html
Might be it will be helped for others.
i did something like this (using the intl package)
final date = '7/10/1996';
final month = DateFormat.LLLL().format(DateTime.parse(date));
LLLL in the code above is date format skeleton meaning 'stand alone month', other date formatter is presented here
add String date as a parameter
DateTime.prase(String userString);
If you have a date and time string in a specific format, you can convert it to a DateTime object by using the parse() method. For example, if you have a string that contains “12/03/2019 9:45 AM”, you can use the parse() method to convert it to a DateTime object like this:
var dateTimeString = “12/03/2019 9:45 AM”;
var dateTimeObject = DateTime.parse(dateTimeString);
print(dateTimeObject); // 12/03/2019 09:45:00.000
The parse() method is very versatile and can handle a variety of different formats. If your string doesn’t follow a strict format, you can use tryParse() instead. This method will return null if it fails to parse the string.
for detail click here
https://mycodingwork.com/flutter-convert-string-to-datetime/
String startdate1="10/31/2022";
String endate1="11/02/2022";
DateTime start = new DateFormat("MM/dd/yyyy").parse(startdate1);
DateTime end = new DateFormat("MM/dd/yyyy").parse(enddate1);
DateTime s = DateTime(start.year, start.month, start.day);
DateTime to = DateTime(end.year, end.month, end.day);
int day= (to.difference(s).inHours / 24).round()+1;

How to parse a String of "hh:mm:ss.SSS" into a DateTime

The format of "new DateTime.now()" will print following output:
2015-05-20 07:34:43.018
By having only the time as a String in the correct format ("07:34:43.018"), how do I parse the time to a DateTime object? The usage of the intl package does not support the mentioned format AFAIK.
By prefixing the Date in front of the time, DateTime would be able to parse the given String.
DateTime parse(String timeValue) {
var prefix = '0000-01-01T';
return DateTime.parse(prefix + timeValue);
}
If you also only want to display the time afterwards, format your DateTime variable accordingly:
String format(DateTime value) {
return "${value.hour}:${value.minute}:${value.second}.${value.millisecond}";
}
I've mainly had to parse the String for calculations (difference) with other time values.

How do I put string format in querystring

Hi I have date in database which is storing in below format : 04/02/2014 00:00:00
Now I need to retrieve this date by removing zeros using string format that is in below format :
04/02/2014.
Below is my asp.net mvc code I am using to retrieve date :
if (scheduleresults.schedule_StartDate != null && scheduleresults.schedule_starttime != null)
{
sbs.Append("<td>" + scheduleresults.schedule_StartDate + scheduleresults.schedule_starttime + "</td>");
}
In above code, startdate contains date.
How do i put string format for above code ??
Now I need to retrieve this date by removing zeros using string format
that is in below format : 04/02/2014.
This should work -
string date = "04/02/2014 00:00:00";
DateTime dt = DateTime.ParseExact(date, "dd/MM/yyyy hh:mm:ss", CultureInfo.InvariantCulture);
Console.WriteLine(dt.ToString("dd/MM/yyyy"));
will display - 04/02/2014
How do i put string format for above code ??
You should code something like this -
scheduleresults.schedule_StartDate.ToString("dd/MM/yyyy");
if it's a datetime object type you can do this
CultureInfo culture = new CultureInfo("pt-BR");
Console.WriteLine(thisDate.ToString("d", culture)); // Displays 15/3/2008
check out http://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx

Blackberry String to Date

I have a String that contain a date, its format is "ddMMyyyy" (for example 21012012 is the current date (21/01/2012))
I want to convert it to a Date object.
This code works for only for "DDMMYYYY" type;
Give like this:
Date date=new Date(HttpDateParser.parse(dateChange("23012012")));
System.out.println("==============Date Object: "+date.getTime());
And the code for dateChange() method is:
public static String dateChange(String string)
{
String str="";
str=string.substring(4, string.length())+"-"+string.substring(2, 4)+"-"+string.substring(0, 2);
return str;//Then I will get here like"2012-01-23";
}
You want Date object;
Then "date" object is in your hand;

Resources