DateTime and String

by Victor 17. March 2010 00:00

 

Convert String to DateTime

1. User CultureInfo class to parse the DateTime string

CultureInfo cultEnUs = new CultureInfo("en-US");
DateTime myDate = Convert.ToDateTime("03/22/2010", cultEnUs.DateTimeFormat);

2. User the DateTimeFormatInfo class to parse the DateTime string

string strDate = "21-07-2006";
DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
dtfi.ShortDatePattern = "dd-MM-yyyy";
dtfi.DateSeparator = "-";
DateTime objDate = Convert.ToDateTime(strDate, dtfi);

 

Convert DateTime to String

1. Use the DateTime.ToString Method.
This is the easiest way to convert DateTime to a string.

Example:

DateTime.Now.ToString("yyyy-MM-dd");

2. User the String.Format Method.

There are following custom format specifies y (year), M (month), d (day), h (hour 12), H (hour 24), m (minute), s (second), f (second fraction), F (second fraction, trailing zeroes are trimmed), t (P.M or A.M) and z (time zone).

Following examples demonstrate how are the format specifies rewritten to the output.

// create date time 2008-03-09 16:05:07.123

DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);

String.Format("{0:y yy yyy yyyy}", dt); // "8 08 008 2008" year
String.Format("{0:M MM MMM MMMM}", dt); // "3 03 Mar March" month
String.Format("{0:d dd ddd dddd}", dt); // "9 09 Sun Sunday" day
String.Format("{0:h hh H HH}", dt); // "4 04 16 16" hour 12/24
String.Format("{0:m mm}", dt); // "5 05" minute
String.Format("{0:s ss}", dt); // "7 07" second
String.Format("{0:f ff fff ffff}", dt); // "1 12 123 1230" sec.fraction
String.Format("{0:F FF FFF FFFF}", dt); // "1 12 123 123" sec.fraction without zeroes
String.Format("{0:t tt}", dt); // "P PM" A.M. or P.M.
String.Format("{0:z zz zzz}", dt); // "-6 -06 -06:00" time zone

 

You can use also date separator / (slash) and time Separator : (colon). These characters will be rewritten to characters defined in the current DateTimeFormatInfo.DateSeparator and DateTimeFormatInfo.TimeSeparator.

// date separator in german culture is "." (so "/" changes to ".")

String.Format("{0:d/M/yyyy HH:mm:ss}", dt); // "9/3/2008 16:05:07" - English (en-US)
String.Format("{0:d/M/yyyy HH:mm:ss}", dt); // "9.3.2008 16:05:07" - German (de-DE)

Here are some examples of custom date and time formatting:

// month/day numbers without/with leading zeroes
String.Format("{0:M/d/yyyy}", dt);            // "3/9/2008"
String.Format("{0:MM/dd/yyyy}", dt);          // "03/09/2008"

// day/month names
String.Format("{0:ddd, MMM d, yyyy}", dt);    // "Sun, Mar 9, 2008"
String.Format("{0:dddd, MMMM d, yyyy}", dt);  // "Sunday, March 9, 2008"

// two/four digit year
String.Format("{0:MM/dd/yy}", dt);            // "03/09/08"
String.Format("{0:MM/dd/yyyy}", dt);          // "03/09/2008"

Standard DateTime Formatting

In DateTimeFormatInfo there are defined standard patterns for the current culture. For example property ShortTimePattern is string that contains value h:mm tt for en-US culture and value HH:mm for de-DE culture.

Here are patterns defined in DateTimeFormatInfo and their values for en-US culture. First column contains format specifies for the String.Format method. (*) = culture independent

Specifier DateTimeFormatInfo property Pattern value (for en-US culture)
t ShortTimePattern h:mm tt
d ShortDatePattern M/d/yyyy
T LongTimePattern h:mm:ss tt
D LongDatePattern dddd, MMMM dd, yyyy
f (combination of D and t) dddd, MMMM dd, yyyy h:mm tt
F FullDateTimePattern dddd, MMMM dd, yyyy h:mm:ss tt
g (combination of d and t) M/d/yyyy h:mm tt
G (combination of d and T) M/d/yyyy h:mm:ss tt
m, M MonthDayPattern MMMM dd
y, Y YearMonthPattern MMMM, yyyy
r, R RFC1123Pattern ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (*)
s SortableDateTi mePattern yyyy'-'MM'-'dd'T'HH':'mm':'ss (*)
u UniversalSorta bleDateTimePat tern yyyy'-'MM'-'dd HH':'mm':'ss'Z' (*)

Following examples show usage of standard format specifiers in String.Format method and the resulting output.

String.Format("{0:t}", dt); //"4:05 PM" ShortTime
String.Format("{0:d}", dt); //"3/9/2008" ShortDate
String.Format("{0:T}", dt); //"4:05:07 PM" LongTime
String.Format("{0:D}", dt); //"Sunday, March 09, 2008" LongDate
String.Format("{0:f}", dt); //"Sunday, March 09, 2008 4:05 PM" LongDate+ShortTime
String.Format("{0:F}", dt); //"Sunday, March 09, 2008 4:05:07 PM" FullDateTime
String.Format("{0:g}", dt); //"3/9/2008 4:05 PM" ShortDate+ShortTime
String.Format("{0:G}", dt); //"3/9/2008 4:05:07 PM" ShortDate+LongTime
String.Format("{0:m}", dt); //"March 09" MonthDay
String.Format("{0:y}", dt); //"March, 2008" YearMonth
String.Format("{0:r}", dt); //"Sun, 09 Mar 2008 16:05:07 GMT" RFC1123
String.Format("{0:s}", dt); //"2008-03-09T16:05:07" SortableDateTime
String.Format("{0:u}", dt); //"2008-03-09 16:05:07Z" UniversalSortableDateTime

Tags:

Technology

Basic about SharePoint Query

by Victor 12. March 2010 10:14

When querying a list in SharePoint, the SPList.GetItems method is a simple and relatively easy way to return a list of records from the SharePoint List (returned as a SPListItemCollection). This is implemented simply as follows:

    using (SPWeb currentWeb = currentSite.OpenWeb(_RelativeWebUrl))
    {
           SPList currentList = currentWeb.Lists[_ListName]; 
           SPListItemCollection items = currentList.GetItems();
    }

As you progress a bit, you will find that you won't necessarily want to return all the items in a list. And the performance is not good when your list has a large amount of items. That's when using the SPQuery object comes in real handy. Simply write the CAML query for the code that you wish to return, then pass the SPQuery object to the GetItems method. This should then return the exact subset of data that you were after. (

    using (SPWeb currentWeb = currentSite.OpenWeb(_RelativeWebUrl))
    {
           SPList currentList = currentWeb.Lists[_ListName];
           SPQuery query = new SPQuery();                   
           query.Query = String.Format("<Where><Eq><FieldRef Name=\"{0}\" /><Value Type=\"{1}\">{2}</Value></Eq></Where>",_InternalName, _Type,_Value);    
           SPListItemCollection items = currentList.GetItems(query);
    }

HOWEVER, you must be careful that you write your CAML code correctly, or the method will return all records! This really surprised me and is a strange 'feature' one would think - I personally would have thought this should have returned NO records if it the query was incorrect. So basically if you find that every time you run a query there is a full record set returning, then check your CAML query...

U2U CAML Query Builder - It is a wonderful tool to test your CAML query.

Tags:

Technology | Sharepoint

Microsoft's New Body Computer

by Victor 5. March 2010 14:35

Microsoft's new concept for muscle-computer interface looks so cool. Really want to try it now.

Original Post: New, Natural User Interfaces

 

Tags:

Technology

Ping Services

by Victor 4. March 2010 23:39

In blogging, ping is an XML-RPC-based push mechanism by which a weblog notifies a server that its content has been updated.[1] An XML-RPC signal is sent to one or more "ping servers," which can then generate a list of blogs that have new material. Many blog authoring tools automatically ping one or more servers each time the blogger creates a new post or updates an old one. For example, Wordpress users can modify their ping list on the Control Panel, then Options, then Writing. In general, The bigger your ping list the higher the chances of receiving traffic from those sources.

Open ping servers, like Moreover Technologies' Weblogs.com, let other web services subscribe to a list of blogs that have recently pinged them. Blog search engines can provide fresh results very quickly by polling only the newly-updated blogs. Similarly, aggregators use results from ping servers to tell subscribers which items on their subscription lists have fresh material.

In addition to open ping servers, there are also proprietary ping servers that gather information only for their own applications. Most of the major blog search engines operate such ping servers.Unlike open ping servers, proprietary servers with their own subscription applications have no incentive to share their received ping data directly with other servers, which may offer competing services. As these servers do not share their data, bloggers have to ping a large number of individual servers to receive the desired publicity. As a result, bloggers have turned to services such as Ping-o-matic, which pings multiple proprietary ping servers.

But like other service in the internet, spam become a problem to the ping service as well. The ping spam or sping attempts to direct readers to web pages that are not, in fact, recent blog posts. 

Examples:
Product vendors who use a weblog-like format to post product ads, meaningless batches of Google keywords, etc.
Software vendors, who sell scripts that make automated "weblog postings" every hour around the clock.
Creators of ping spam or spam blogs may hope to benefit by creating pages to turn up in web searches for popular keywords. Typically, an individual spam post links to some external page that displays Google ads or offers a product for sale.

Here is list of Ping Services you can add to your blog:

http://blogsearch.google.com/ping/RPC2
http://1470.net/api/ping
http://api.feedster.com/ping
http://api.moreover.com/RPC2
http://api.moreover.com/ping
http://api.my.yahoo.com/RPC2
http://api.my.yahoo.com/rss/ping
http://bblog.com/ping.php
http://bitacoras.net/ping
http://blog.goo.ne.jp/XMLRPC
http://blogdb.jp/xmlrpc
http://blogmatcher.com/u.php
http://bulkfeeds.net/rpc
http://coreblog.org/ping/
http://mod-pubsub.org/kn_apps/blogchatt
http://www.lasermemory.com/lsrpc/
http://ping.amagle.com/
http://ping.bitacoras.com
http://ping.blo.gs/
http://ping.bloggers.jp/rpc/
http://ping.cocolog-nifty.com/xmlrpc
http://ping.blogmura.jp/rpc/
http://ping.exblog.jp/xmlrpc
http://ping.feedburner.com
http://ping.myblog.jp
http://ping.rootblog.com/rpc.php
http://ping.syndic8.com/xmlrpc.php
http://ping.weblogalot.com/rpc.php
http://ping.weblogs.se/
http://pingoat.com/goat/RPC2
http://rcs.datashed.net/RPC2/
http://rpc.blogbuzzmachine.com/RPC2
http://rpc.blogrolling.com/pinger/
http://rpc.icerocket.com:10080/
http://rpc.newsgator.com/
http://rpc.pingomatic.com
http://rpc.technorati.com/rpc/ping
http://rpc.weblogs.com/RPC2
http://topicexchange.com/RPC2
http://trackback.bakeinu.jp/bakeping.php
http://www.a2b.cc/setloc/bp.a2b
http://www.bitacoles.net/ping.php
http://www.blogdigger.com/RPC2
http://www.blogoole.com/ping/
http://www.blogoon.net/ping/
http://www.blogpeople.net/servlet/weblogUpdates
http://www.blogroots.com/tb_populi.blog?id=1
http://www.blogshares.com/rpc.php
http://www.blogsnow.com/ping
http://www.blogstreet.com/xrbin/xmlrpc.cgi
http://www.mod-pubsub.org/kn_apps/blogchatter/ping.php
http://www.newsisfree.com/RPCCloud
http://www.newsisfree.com/xmlrpctest.php
http://www.popdex.com/addsite.php
http://www.snipsnap.org/RPC2
http://www.weblogues.com/RPC/
http://xmlrpc.blogg.de
http://xping.pubsub.com/ping/

Tags:

Technology

Port Blocking for Firefox and Chrome

by Victor 4. March 2010 15:32

Yesterday, I set up a testing website on my testing server with port 6000. I tried to browse it with Firefox. I got an error like "This address is restricted. This address uses a network port which is normally used for purposes other than Web browsing. Firefox has cancelled the request for your protection. " Then I tried in the Chrome, I got similar error message as wel. Surprisingly, IE worked fine. This was totally new to me. Then I found out the fact that Mozilla blocks some ports for the security reason. Here is a list of port they blocked :

Port Service

1 - tcpmux 7 - echo 9 - discard 11 - systat
13 - daytime 15 - netstat 17 - qotd 19 - chargen
20 - ftp data 21 - ftp control 22 - ssh 23 - telnet
25 - smtp 37 - time 42 - name 43 - nicname
53 - domain 77 - priv-rjs 79 - finger 87 - ttylink
95 - supdup 101 - hostriame 102 - iso-tsap 103 - gppitnp
104 - acr-nema 109 - POP2 110 - POP3 111 - sunrpc
113 - auth 115 - sftp 117 - uucp-path 119 - NNTP
123 - NTP 135 - loc-srv / epmap 139 - netbios 143 - IMAP2
179 - BGP 389 - LDAP 465 - SMTP+SSL 512 - print / exec
513 - login 514 - shell 515 - printer 526 - tempo
530 - courier 531 - chat 532 - netnews 540 - uucp
556 - remotefs 563 - NNTP+SSL 587 - submission 601 - syslog
636 - LDAP+SSL 993 - IMAP+SSL 995 - POP3+SSL 2049 - nfs
4045 - lockd 6000 - X11    

For more detail about this you can visit Mozilla's website.

Resolution:

1. If you can decide how to host your website, try to avoid use these ports when you config your website.

2. If you are end user, you can change your configuration to override this setting.

For Firefox, enter about:config in the address bar, and then right-click anywhere in the lower portion of the resulting page and select New and then String from the context menu. The name you need to enter is network.security.ports.banned.override, and the value is the port(s) you want to open. If you have more than one, enter them as a comma-separated list. Or you can just type "1-65535", that will open all ports.

 

Tags: , ,

General | Technology

Microsoft TechEd 2009 New Zealand

by Victor 16. September 2009 00:19

 

Microsoft TechEd  2009 New Zealand has been finished today. To be honest, this year's event is quite disappointing. I can only give it 6 out of 10. One of the most common words I heard is "Sorry, I cannot talk about this right now". There are not a lot of new stuffs from a developer's point of view. In my last session "OFC301 - Deep dive into SharePoint features and solutions", it looks like the speaker was doing SharePoint Development 101. Come on!  :( Maybe Microsoft should follow Gen-I's example, found some nice looking girls to make juice for everyone. That would be more fun than sitting there and listening to something really basic.  But among all sessions I attended, I found those sessions presented by Michael Howard are really good. He really inspired on how I should write secure code as a developer. Apart from that, I also quite excited about the Windows Identity Foundation. But that about all, hopefully there will be a more exciting TechEd next year.

 

Tags:

Technology

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar

Month List