NSDate to NSString returns nil - nsstring

NSString *dateString = #"13-8-06 10:53:54 +0300";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:#"yy-MM-dd HH:mm 'ZZ'"];
NSDate *dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:dateString];
dateFromString is nil. Does anyone know which format should I give to NSDateFormatter??

Try this
NSString *dateString = #"13-8-06 10:53:54 +0300";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yy HH:mm:ss ZZZ"];
NSDate *date =[dateFormatter dateFromString:dateString];
NSLog(#"%#",date);
[dateFormatter setDateFormat:#"YYYY-MM-dd HH:mm:ss ZZZ"];
dateString =[dateFormatter stringFromDate:date];
NSLog(#"%#",dateString);

You have to set date format as
#"dd-MM-yy HH:mm:ss ZZZ"

NSString *dateString = #"13-8-06 10:53:54 +0300";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yy-M-dd HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(#"%#",date);

Try this [dateFormatter setDateFormat:#"yy-MM-dd HH:mm:ss ZZ"];

Related

How can show Persian Calendar in DevExpress DateEdit

I want to use DevExpress DateEdit with Persian Calendar in visual studio. I tried blew code but it wasn't work. Please help me. Thanks in advance.
Public Sub ToPersian(DateTimePicker As DevExpress.XtraEditors.DateEdit)
Dim shamsi As New System.Globalization.PersianCalendar
Dim PersianToday As DateTime = Convert.ToDateTime(shamsi.GetYear(DateTime.Now) & "/" & shamsi.GetMonth(DateTime.Now) & "/" & shamsi.GetDayOfMonth(DateTime.Now))
System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("fa-IR")
System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo("fa-IR")
System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.MonthNames = New String() {"فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", ""}
System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.MonthGenitiveNames = New String() {"فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", ""}
System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.AbbreviatedMonthNames = New String() {"فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", ""}
System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.AbbreviatedMonthGenitiveNames = New String() {"فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", ""}
System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.AbbreviatedDayNames = New String() {"ی", "د", "س", "چ", "پ", "ج", "ش"}
System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.ShortestDayNames = New String() {"ی", "د", "س", "چ", "پ", "ج", "ش"}
System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.DayNames = New String() {"یکشنبه", "دوشنبه", "ﺳﻪشنبه", "چهارشنبه", "پنجشنبه", "جمعه", "شنبه"}
System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.AMDesignator = "ق.ظ"
System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.PMDesignator = "ب.ظ"
System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.ShortTimePattern = "HH:mm"
System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.DateSeparator = "/"
System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.FullDateTimePattern = "dd/MM/yyyy HH:mm"
System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Saturday
System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy"
System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.LongDatePattern = "dd/MM/yyyy HH:mm"
System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.SetAllDateTimePatterns(New String() {"dd/MM/yyyy"}, "d"c)
System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.SetAllDateTimePatterns(New String() {"dddd, dd MMMM yyyy"}, "D"c)
System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.SetAllDateTimePatterns(New String() {"yyyy MMMM"}, "y"c)
System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.SetAllDateTimePatterns(New String() {"yyyy MMMM"}, "Y"c)
DateTimePicker.EditValue = PersianToday
DateTimePicker.Properties.TodayDate = PersianToday
DateTimePicker.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime
DateTimePicker.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime
DateTimePicker.Properties.FirstDayOfWeek = DayOfWeek.Saturday
DateTimePicker.Properties.EditMask = "yyyy/MM/dd"
DateTimePicker.Properties.Mask.UseMaskAsDisplayFormat = True
DateTimePicker.RightToLeft = RightToLeft.Yes
End Sub
Just add following lines of the codes into your Program.cs file in your project. it works fine in my projects:
static void Main()
{
CultureInfo culture = CultureInfo.CreateSpecificCulture("fa-IR");
// The following line provides localization for the application's user interface.
Thread.CurrentThread.CurrentUICulture = culture;
// The following line provides localization for data formats.
Thread.CurrentThread.CurrentCulture = culture;
// Set this culture as the default culture for all threads in this application.
// Note: The following properties are supported in the .NET Framework 4.5+
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}

Replace hexadecimal value in string

I'm getting error '', hexadecimal value 0x03, is an invalid character. Line 440033, position 80. Also see image:
It happens when I try to load the XML file into an XMLDocument.
I tried removing this hexadecimal value in a variety of ways, but none of them work:
Regex.Replace(responseString, "0x03", String.Empty)
Regex.Replace(responseString, "\0x03", String.Empty)
responseString = Regex.Replace(responseString, "0x03", String.Empty)
responseString = Regex.Replace(responseString, "\0x03", String.Empty)
responseString = responseString.Replace("\0x03", String.Empty)
responseString = responseString.Replace("0x03", String.Empty)
What am I doing wrong?
I now solved it as suggested in this post: C# Hexadecimal to char
For me that was:
responseString = responseString.Replace(System.Convert.ToChar(System.Convert.ToUInt32("0x03", 16)), "")

Get Month and Year from textBox in asp.net

I have a textbox from which I choose a date. Based on the selection, I have to get the respective month and year and save it as integer in the database. How do I do this?
If your are using jquery then add option for dateformat and you will have desired format you want
$(function () {
$('.date-picker').datepicker({
changeMonth: true,
changeYear: true,
showButtonPanel: true,
dateFormat: 'MM yy',
onClose: function (dateText, inst) {
var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
$(this).datepicker('setDate', new Date(year, month, 1));
}
});
});
Here's is fiddle for same.
Why don't you use a DateTimePicker * instead?
However, if you insist on the TextBox, use DateTime.TryParse to validate and parse a date. Once you have the DateTime you can use it's Month and Year properties.
DateTime dt;
if(DateTime.TryParse(txtDate.Text, out dt))
{
int month = dt.Month;
int year = dt.Year;
}
else
{
MessageBox.Show("Enter a valid date");
}
* Edit since you are using ASP.NET you could use the Calendar control instead of the DateTimePicker. If you're using ASP.NET-Ajax a CalendarExtender is also a good alternative.
From the commets i see that you're using VB.NET ....
Dim dt As Date
If Date.TryParse(txtDate.Text, dt) Then
Dim month As Int32 = dt.Month
Dim year As Int32 = dt.Year
Else
MessageBox.Show("Enter a valid date")
End If
To get the month and year I used the following code.
Dim nStartMonth = Month(txtStartDate.Text)
Dim nStartYear = Year(txtStartDate.Text)
Thanks all for your time and help! :)

Trouble using NSDateFormatter?

I'm trying to use NSDateFormatter to convert an NSString like 2013-02-08T10:50:00.000 to 10:50AM, no matter what I do I can't seem to get the AM or to get it to work with something over 12 (2013-02-08T15:00:00.000) any ideas?
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH-mm"];
NSString *datelocalformat= [dateFormatter stringFromDate:DepartTimeString];
NSDate *today = [NSDate date];//get today's date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
NSString *dateString = [dateFormatter stringFromDate:today];
[dateFormatter release];
// Get time - display in 12HR
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setTimeStyle:NSDateFormatterLongStyle];
NSString *timeString = [timeFormatter stringFromDate:today];
[timeFormatter release];

Converting Date From TextBox To Date In ASP.Net

I want to get the difference of date from Two TextBox say to and from dates..
My Code is here
Dim ts As TimeSpan
ts = CDate(txtTo.Text) - CDate(txtFrom.Text)
txtDays.Text = ts.Days() + 1
but this code is throwing an error like this
Conversion from string "16/11/2011" to type 'Date' is not valid.
In general, try to avoid using old VB functions like CDate. In this case, you should use the Parse method:
Dim ts As TimeSpan
ts = DateTime.Parse(txtTo.Text) - DateTime.Parse(txtFrom.Text)
Or, if you know the date format in advance:
Dim fmt As String = "dd/MM/yyyy"
Dim ts As TimeSpan
ts = DateTime.ParseExact(txtTo.Text, fmt, Nothing) - DateTime.ParseExact(txtFrom.Text, fmt, Nothing)
Try using ParseExact like:
Dim s As String = txtFrom.Text.trim
Dim pattern As String = "dd/MM/yyyy"
Dim parsedDate As Date = Date.ParseExact(s, pattern, Nothing)
As that way you can specify the format of the string that is used.
More direct to your example:
Dim pattern As String = "dd/MM/yyyy"
ts = Date.ParseExact(txtTo.Text, pattern, Nothing) - Date.ParseExact(txtFrom.Text, pattern, Nothing)
You can directly cast a string to DateTime. You need to use the DateTime.Parse function to parse a string to a DateTime.
The syntax can be found here.
Use overloaded version of the function:
DateTime value = DateTime.Parse(txtFrom.Text, new CultureInfo("gu-IN", false));
where "gu-IN" is for Gujarati (India)

Resources