Trouble using NSDateFormatter? - nsstring

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];

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());
}

How to format pdf or excel with itextsharper from Asp.net Control

I want to generate report in pdf,excel format. I have used asp.net repeater control for data binding. I am using iTextSharp. I rendered, page control to pdf and excel format.
Code:
StringWriter sw2 = new StringWriter();
HtmlTextWriter hw2 = new HtmlTextWriter(sw2);
this.rptBillReport.RenderControl(hw2);
Paragraph report = new Paragraph();
using (StringReader sr2 = new StringReader(sw2.ToString()))
{
//Parse and get a collection of elements
List<IElement> elements2 = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(sr2, null);
foreach (IElement val2 in elements2)
{
//Add those elements to the paragraph
report.Add(val2);
}
}
Document pdfDoc = new Document(iTextSharp.text.PageSize.A4,
30f, 30f, 30f, 0.0f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
//htmlparser.Parse(sr);
pdfDoc.Add(report);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
My Question here is:
How do I format the report, adding border and other style in both pdf and excel? It is not showing on my current report.
Thanks,
I added stylesheet like this:
StyleSheet styles= new StyleSheet();
styles.LoadTagStyle("#rptBillReport", "height", "30px");
styles.LoadTagStyle("#rptBillReport", "font-weight", "bold");
styles.LoadTagStyle("#rptBillReport", "font-family", "Cambria");
styles.LoadTagStyle("#rptBillReport", "font-size", "20px");
styles.LoadTagStyle("#rptBillReport", "background-color", "white");
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
htmlparser.SetStyleSheet(styles);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
It worked.

How to convert panel into pdf with same font and style using iTextSharp

I am using iTextSharp dll to convert panel in aspx page. Panel have controls like label, textboxes along with gridview and listview in it. The problem is this that after converting panel to pdf format i lost all the formatting of listview/gridview/other controls. I am using below code`
string file = txtenqid.Text + ".pdf";
string attachment = string.Format("attachment;filename={0}", file);
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/pdf";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
Panel1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
iTextSharp.text.Document pdfdoc = new iTextSharp.text.Document();
pdfdoc.SetMargins(0f, 0f, 0f, 0f);
pdfdoc.SetPageSize(iTextSharp.text.PageSize.A3);
HTMLWorker htmlparser = new HTMLWorker(pdfdoc);
PdfWriter.GetInstance(pdfdoc, Response.OutputStream);
pdfdoc.Open();
htmlparser.Parse(sr);
pdfdoc.Close();
please do the needful changes in the code so that i got my desired result. Thanks in advance
Formatting could be lost because the pdf generated may not be as wide as the panel itself. Try setting the pdf page size to A4 instead of A3
iTextSharp.text.Document pdfdoc = new Document(new Rectangle(288f, 144f), 10, 10, 10, 10);
pdfdoc.SetPageSize(iTextSharp.text.PageSize.A4);
Also rotating it to landscape may solve your issue
pdfdoc.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());

How to set the page size to Envelope size with Landscape orientation?

I have problem on creating .pdf file with the Page settings as Envelope ,landscape format.
Here is my Code to convert the asp page into pdf in Itextsharp
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Receipt.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
bind_Data();
this.Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4.Rotate(), 10f, 10f, 100f, 0f);
//here i need to set Pagesize as Envelope.
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
i googled it but i couldn't find Envelope size.How do i set the page size as Envelope ,Landscape in dynamically
Thanks In Advance
You are creating an A4 document in landscape format with this line:
Document pdfDoc = new Document(PageSize.A4.Rotate(), 10f, 10f, 100f, 0f);
If you want to create a document in envelope format, you shouldn't create an A4 page, instead you should do this:
Document pdfDoc = new Document(envelope, 10f, 10f, 100f, 0f);
In this line, envelope is an object of type Rectangle.
There is no such thing as the envelope size. There are different envelope sizes to choose from: http://www.paper-papers.com/envelope-size-chart.html
For instance, if you want to create a page with the size of a 6-1/4 Commercial Envelope, then you need to create a rectangle that measures 6 by 3.5 inch. The measurement system in PDF doesn't use inches, but user units. By default, 1 user unit = 1 point, and 1 inch = 72 points.
Hence you'd define the envelope variable like this:
Rectangle envelope = new Rectangle(432, 252);
Because:
6 inch x 72 points = 432 points (the width)
3.5 inch x 72 points = 252 points (the height)
If you want a different envelope type, you have to do the calculations of the dimensions of that envelope format.

NSDate to NSString returns nil

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"];

Resources