<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Useful scripts &#124; Basic and Advanced Day to day used scripts</title>
	<atom:link href="http://usefulscripts.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://usefulscripts.wordpress.com</link>
	<description>the scripts which are found rarely on the internet but needed often</description>
	<lastBuildDate>Fri, 03 Jun 2011 11:28:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='usefulscripts.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Useful scripts &#124; Basic and Advanced Day to day used scripts</title>
		<link>http://usefulscripts.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://usefulscripts.wordpress.com/osd.xml" title="Useful scripts &#124; Basic and Advanced Day to day used scripts" />
	<atom:link rel='hub' href='http://usefulscripts.wordpress.com/?pushpress=hub'/>
		<item>
		<title>How to use appSettings and connectionStrings in C#</title>
		<link>http://usefulscripts.wordpress.com/2010/12/27/how-to-use-appsettings-and-connectionstrings-in-c/</link>
		<comments>http://usefulscripts.wordpress.com/2010/12/27/how-to-use-appsettings-and-connectionstrings-in-c/#comments</comments>
		<pubDate>Mon, 27 Dec 2010 04:54:37 +0000</pubDate>
		<dc:creator>ubk</dc:creator>
				<category><![CDATA[C Sharp]]></category>
		<category><![CDATA[C# Script]]></category>
		<category><![CDATA[Microsoft Track]]></category>
		<category><![CDATA[app.config]]></category>
		<category><![CDATA[AppSettings]]></category>
		<category><![CDATA[ConfigurationManager]]></category>
		<category><![CDATA[connectionStrings]]></category>
		<category><![CDATA[system.configuration]]></category>
		<category><![CDATA[web.config]]></category>

		<guid isPermaLink="false">http://usefulscripts.wordpress.com/?p=109</guid>
		<description><![CDATA[For the developers in C# the most useful file is the App.Config or the Web.Config file and the most used settings in appSettings. You can use any name for the configuration file. Even you can use Multiple configuration file in you application. This configuration file is an XML file having  .config extension. The .NET Framework [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usefulscripts.wordpress.com&amp;blog=1889208&amp;post=109&amp;subd=usefulscripts&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>For the developers in C# the most useful file is the App.Config or the Web.Config file and the most used settings in appSettings. You can use any name for the configuration file. Even you can use Multiple configuration file in you application.</p>
<p>This configuration file is an XML file having  .config extension. The .NET Framework          provides the <strong>System.Configuration.ConfigurationManager.AppSettings</strong> namspace which lets you to retrieve appSettings elements          from the configuration file. Data is stored in key/value pairs as shown in this          sample .config file:</p>
<pre>&lt;appSettings&gt;
&lt;add key="ServiceName" value="Test  Service"/&gt;
&lt;add key="ServiceDisplayName" value="Test Display Service"/&gt;
&lt;add key="ServiceDescription" value="Test Description"/&gt;
&lt;/appSettings&gt;</pre>
<p>Now, to read this appSettings you need to include <strong>System.Configuration </strong>(2.0) in to your application. <strong></strong></p>
<p>Here is a sample code that reads the required appSettings for your application:</p>
<p>String _serviceName = ConfigurationManager.AppSettings["ServiceName"].Trim();<br />
String _serviceDisplayName = ConfigurationManager.AppSettings["ServiceDisplayName"].Trim();<br />
String _serviceDesc = ConfigurationManager.AppSettings["ServiceDescription"].Trim();</p>
<p>Now, you can use all these 3 variables as you like in your application. You can also use a NULL value check before assignment like this:</p>
<p>String _serviceDesc = (null != ConfigurationManager.AppSettings["ServiceDescription"].Trim())<br />
? ConfigurationManager.AppSettings["ServiceDescription"].Trim() : &#8220;Default Value&#8221;;</p>
<p>The following example code reads all the appSettings from the config file in your application:</p>
<pre>    <span style="color:#99cc00;">// Get the AppSettings section.        </span>
<span style="color:#99cc00;">    // This function uses the AppSettings property</span>
<span style="color:#99cc00;">    // to read the appSettings configuration </span>
<span style="color:#99cc00;">    // section.</span>
    public static void ReadAppSettings()
    {
      try
      {
        // Get the AppSettings section.
        NameValueCollection appSettings =
           ConfigurationManager.AppSettings;

        // Get the AppSettings section elements.
        Console.WriteLine();
        Console.WriteLine("Using AppSettings property.");
        Console.WriteLine("Application settings:");

        if (appSettings.Count == 0)
        {
          Console.WriteLine("[ReadAppSettings: {0}]",
          "AppSettings is empty Use GetSection command first.");
        }
        for (int i = 0; i &lt; appSettings.Count; i++)
        {
          Console.WriteLine("#{0} Key: {1} Value: {2}",
            i, appSettings.GetKey(i), appSettings[i]);
        }
      }
      catch (ConfigurationErrorsException e)
      {
        Console.WriteLine("[ReadAppSettings: {0}]",
            e.ToString());
      }
    }</pre>
<p>In some cases you might want to save user input to your appSettings. So, you need to create new appSettings Dynamically or Change an existing one. The following code section demonstrates how to create a new appSettings section. I believe the modification part can be done by looking at these 2 examples:</p>
<pre>    <span style="color:#99cc00;">// Create the AppSettings section.</span>
<span style="color:#99cc00;">    // The function uses the GetSection(string)method </span>
<span style="color:#99cc00;">    // to access the configuration section. </span>
<span style="color:#99cc00;">    // It also adds a new element to the section collection.</span>

    public static void CreateAppSettings()
    {
      // Get the application configuration file.
      System.Configuration.Configuration config =
        ConfigurationManager.OpenExeConfiguration(
              ConfigurationUserLevel.None);

      string sectionName = "appSettings";

      // Add an entry to appSettings.
      int appStgCnt =
          ConfigurationManager.AppSettings.Count;
      string newKey = "NewKey" + appStgCnt.ToString();

      string newValue = DateTime.Now.ToLongDateString() +
        " " + DateTime.Now.ToLongTimeString();

      config.AppSettings.Settings.Add(newKey, newValue);

      // Save the configuration file.
      config.Save(ConfigurationSaveMode.Modified);

      // Force a reload of the changed section. This
      // makes the new values available for reading.
      ConfigurationManager.RefreshSection(sectionName);

      // Get the AppSettings section.
      AppSettingsSection appSettingSection =
        (AppSettingsSection)config.GetSection(sectionName);

      Console.WriteLine();
      Console.WriteLine("Using GetSection(string).");
      Console.WriteLine("AppSettings section:");
      Console.WriteLine(
        appSettingSection.SectionInformation.GetRawXml());
    }</pre>
<p><strong>ConnectionStrings:</strong></p>
<p>To Read a connectionString from app.config / web.config you can use the following code:</p>
<pre>public string GetConnectionString(string str)
{
//variable to hold our return value
string conn = string.Empty;
//check if a value was provided
if (!string.IsNullOrEmpty(str))
{
//name provided so search for that connection
conn = ConfigurationManager.ConnectionStrings[str].ConnectionString;
return conn;
}
//In all other cases return null
return null;
}</pre>
<p>This method will return the named connection string. The Key Name needs to be pass to the method as input , else it will return null.</p>
<p>Assuming you have the following in your config file:</p>
<p>&lt;connectionStrings&gt;<br />
&lt;add name=&#8221;MyConnString1&#8243; connectionString=&#8221;&lt;connection string 1&gt;&#8221; providerName=&#8221;&lt;provider1&gt;&#8221; /&gt;<br />
&lt;add name=&#8221;MyConnString2&#8243; connectionString=&#8221;&lt;connection string 2&gt;&#8221; providerName=&#8221;&lt;provider2&gt;&#8221; /&gt;<br />
&lt;/connectionStrings&gt;</p>
<p>Here, we have 2 separate connectionStrings. You can have as many as you want. To use it, you just need to call the above method with the name like this:</p>
<pre>String _connecitonString = GetConnectionString("MyConnectionString1");

if(null != _connecitonString){
//Your code
//
}</pre>
<p>Now if you want to read all Connection Strings then you need to use the following code:</p>
<pre>    <span style="color:#99cc00;">// Get the ConnectionStrings section.        </span>
<span style="color:#99cc00;">    // This function uses the ConnectionStrings </span>
<span style="color:#99cc00;">    // property to read the connectionStrings</span>
<span style="color:#99cc00;">    // configuration section.</span>
    public static void ReadConnectionStrings()
    {

      // Get the ConnectionStrings collection.
      ConnectionStringSettingsCollection connections =
          ConfigurationManager.ConnectionStrings;

      if (connections.Count != 0)
      {
        Console.WriteLine();
        Console.WriteLine("Using ConnectionStrings property.");
        Console.WriteLine("Connection strings:");

        // Get the collection elements.
        foreach (ConnectionStringSettings connection in
          connections)
        {
          string name = connection.Name;
          string provider = connection.ProviderName;
          string connectionString = connection.ConnectionString;

          Console.WriteLine("Name:               {0}",
            name);
          Console.WriteLine("Connection string:  {0}",
            connectionString);
         Console.WriteLine("Provider:            {0}",
            provider);
        }
      }
      else
      {
        Console.WriteLine();
        Console.WriteLine("No connection string is defined.");
        Console.WriteLine();
      }
    }</pre>
<p>You can read more about <a title="appSettings" href="http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx">appSettings </a>and <a title="ConnectionStrings" href="http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.connectionstrings.aspx" target="_blank">connectionStrings </a>in MSDN.</p>
<br />Filed under: <a href='http://usefulscripts.wordpress.com/category/c-sharp/'>C Sharp</a>, <a href='http://usefulscripts.wordpress.com/category/c-script/'>C# Script</a>, <a href='http://usefulscripts.wordpress.com/category/microsoft-track/'>Microsoft Track</a> Tagged: <a href='http://usefulscripts.wordpress.com/tag/app-config/'>app.config</a>, <a href='http://usefulscripts.wordpress.com/tag/appsettings/'>AppSettings</a>, <a href='http://usefulscripts.wordpress.com/tag/configurationmanager/'>ConfigurationManager</a>, <a href='http://usefulscripts.wordpress.com/tag/connectionstrings/'>connectionStrings</a>, <a href='http://usefulscripts.wordpress.com/tag/system-configuration/'>system.configuration</a>, <a href='http://usefulscripts.wordpress.com/tag/webconfig/'>web.config</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/usefulscripts.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/usefulscripts.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/usefulscripts.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/usefulscripts.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/usefulscripts.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/usefulscripts.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/usefulscripts.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/usefulscripts.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/usefulscripts.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/usefulscripts.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/usefulscripts.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/usefulscripts.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/usefulscripts.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/usefulscripts.wordpress.com/109/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usefulscripts.wordpress.com&amp;blog=1889208&amp;post=109&amp;subd=usefulscripts&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://usefulscripts.wordpress.com/2010/12/27/how-to-use-appsettings-and-connectionstrings-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/95e9ce7eea29d07432c0186177ff983a?s=96&#38;d=wavatar&#38;r=G" medium="image">
			<media:title type="html">ubk</media:title>
		</media:content>
	</item>
		<item>
		<title>How to Get the Time Difference / Total run time of an application in C#</title>
		<link>http://usefulscripts.wordpress.com/2010/12/22/how-to-get-the-time-difference-total-run-time-of-an-application-in-c/</link>
		<comments>http://usefulscripts.wordpress.com/2010/12/22/how-to-get-the-time-difference-total-run-time-of-an-application-in-c/#comments</comments>
		<pubDate>Wed, 22 Dec 2010 18:43:14 +0000</pubDate>
		<dc:creator>ubk</dc:creator>
				<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Application run time]]></category>
		<category><![CDATA[Difference between dates]]></category>
		<category><![CDATA[Time]]></category>
		<category><![CDATA[Timespan]]></category>

		<guid isPermaLink="false">http://usefulscripts.wordpress.com/?p=93</guid>
		<description><![CDATA[In the practical world of coding you&#8217;ll come around a situation when you want to know how long it takes your application to finish. This is very useful to judge the user experience. Depending on the time it takes you can also think of modifying the code to remove extra loops or time consuming methods [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usefulscripts.wordpress.com&amp;blog=1889208&amp;post=93&amp;subd=usefulscripts&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In the practical world of coding you&#8217;ll come around a situation when you want to know how long it takes your application to finish. This is very useful to judge the user experience. Depending on the time it takes you can also think of modifying the code to remove extra loops or time consuming methods so that the application can run faster.</p>
<p>Here is how I did it:</p>
<p>First at the beginning of every coding I have instantiated a new Date object:</p>
<p>DateTime _startdt = DateTime.Now;</p>
<p>and at the end of the application I again instantiate another date object:</p>
<p>DateTime _enddt = DateTime.Now;</p>
<p>Now, we can get the difference between them by just using Timespan class:</p>
<p>TimeSpan ts = _enddt &#8211; _startdt;</p>
<p>You can now display the difference in human readable format:</p>
<p>Console.WriteLine(&#8220;Total Time taken &gt;&gt; &#8221; +<br />
                &#8221; Day :&#8221; + ts.Days +<br />
                &#8221; Hour(s) :&#8221; + ts.Hours +<br />
                &#8221; Minute(s) :&#8221; + ts.Minutes +<br />
                &#8221; Second(s) : &#8221; + ts.Seconds +<br />
                &#8221; Milli Second(s) : &#8221; + ts.Milliseconds<br />
                ); </p>
<p>This is very short but useful trick for us. Hope you&#8217;ll like it.</p>
<br />Filed under: <a href='http://usefulscripts.wordpress.com/category/scripts/'>Scripts</a> Tagged: <a href='http://usefulscripts.wordpress.com/tag/application-run-time/'>Application run time</a>, <a href='http://usefulscripts.wordpress.com/tag/difference-between-dates/'>Difference between dates</a>, <a href='http://usefulscripts.wordpress.com/tag/time/'>Time</a>, <a href='http://usefulscripts.wordpress.com/tag/timespan/'>Timespan</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/usefulscripts.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/usefulscripts.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/usefulscripts.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/usefulscripts.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/usefulscripts.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/usefulscripts.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/usefulscripts.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/usefulscripts.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/usefulscripts.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/usefulscripts.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/usefulscripts.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/usefulscripts.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/usefulscripts.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/usefulscripts.wordpress.com/93/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usefulscripts.wordpress.com&amp;blog=1889208&amp;post=93&amp;subd=usefulscripts&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://usefulscripts.wordpress.com/2010/12/22/how-to-get-the-time-difference-total-run-time-of-an-application-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/95e9ce7eea29d07432c0186177ff983a?s=96&#38;d=wavatar&#38;r=G" medium="image">
			<media:title type="html">ubk</media:title>
		</media:content>
	</item>
		<item>
		<title>How to write a CAML query to find out a list of items with ID</title>
		<link>http://usefulscripts.wordpress.com/2010/12/22/how-to-write-a-caml-query-to-find-out-a-list-of-items-with-id/</link>
		<comments>http://usefulscripts.wordpress.com/2010/12/22/how-to-write-a-caml-query-to-find-out-a-list-of-items-with-id/#comments</comments>
		<pubDate>Wed, 22 Dec 2010 18:01:24 +0000</pubDate>
		<dc:creator>ubk</dc:creator>
				<category><![CDATA[Basic Scripts]]></category>
		<category><![CDATA[C Sharp]]></category>
		<category><![CDATA[C# Script]]></category>
		<category><![CDATA[Microsoft Track]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[CAML]]></category>
		<category><![CDATA[GetListItems]]></category>
		<category><![CDATA[List]]></category>
		<category><![CDATA[Query SharePoint List]]></category>
		<category><![CDATA[View]]></category>

		<guid isPermaLink="false">http://usefulscripts.wordpress.com/?p=89</guid>
		<description><![CDATA[Hi, Few days ago I came around a problem where I have to query a SharePoint list for a bunch of items and I only have the ID for those. The ID field is a counter. I am using the WebService.GetListItems method for the query. By googleing I came to know that I have to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usefulscripts.wordpress.com&amp;blog=1889208&amp;post=89&amp;subd=usefulscripts&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Hi,</p>
<p>Few days ago I came around a problem where I have to query a SharePoint list for a bunch of items and I only have the ID for those. The ID field is a counter. I am using the WebService.GetListItems method for the query.</p>
<p>By googleing I came to know that I have to write the query in the following manner to fetch 1 item:</p>
<p>&lt;Where&gt;<br />
 &lt;Eq&gt;<br />
  &lt;FieldRef Name=&#8217;ID&#8217;/&gt;<br />
  &lt;Value Type=&#8217;Counter&#8217;&gt;&#8221; + _slideid + &#8220;&lt;/Value&gt;<br />
 &lt;/Eq&gt;<br />
&lt;/Where&gt;</p>
<p>Where the slide id is one of the IDs of from the slide list.</p>
<p>The query worked well but I wanted to extend it for n number of items. i.e. If I have a Slide ID array, I want to get the Items in one query. Here is the code snippet that does the trick:</p>
<p>XmlNodeList GetFilteredNodesFromSharepointBySlideID(string _tmpListName, string _tmpViewName, string[] _slideid)<br />
        {<br />
            XmlNodeList _tmplNodeLists = null;</p>
<p>            try<br />
            { </p>
<p>                XmlDocument doc = new XmlDocument();</p>
<p>                XmlNode query = doc.CreateNode(XmlNodeType.Element, &#8220;Query&#8221;, &#8220;&#8221;);</p>
<p>                String _query = &#8220;&lt;Where&gt;&#8221;;<br />
                String _innerQ = &#8220;&#8221;;<br />
                string _tempQ = &#8220;&#8221;;</p>
<p>                for (int i = 0; i &lt; _slideid.Length; i++)<br />
                {<br />
                    _tempQ += &#8220;&lt;Eq&gt;&lt;FieldRef Name=&#8217;ID&#8217;/&gt;&lt;Value Type=&#8217;Counter&#8217;&gt;&#8221; + _slideid[i] + &#8220;&lt;/Value&gt;&lt;/Eq&gt;&#8221;;</p>
<p>                    if (i &gt;= 1)<br />
                    {<br />
                        _innerQ = &#8220;&lt;Or&gt;&#8221; + _tempQ + &#8220;&lt;/Or&gt;&#8221;;<br />
                        _tempQ = _innerQ;<br />
                    }                   <br />
                }</p>
<p>                _query += _tempQ + &#8220;&lt;/Where&gt;&#8221;;</p>
<p>                query.InnerXml = _query;<br />
              </p>
<p>                XmlNode templistItems =<br />
                  MyWebService.GetListItems(_tmpListName, _tmpViewName, query,<br />
                  null, null, null, null); //MyWebService is the Configured Web Service               </p>
<p>                doc.LoadXml(templistItems.OuterXml);</p>
<p>                _tmplNodeLists = doc.GetElementsByTagName(&#8220;z:row&#8221;); // Now you can return the node list as XMLNodes.</p>
<p>             }<br />
            catch (Exception _e)<br />
            {<br />
                throw _e;<br />
            }</p>
<p>            return _tmplNodeLists;<br />
        }</p>
<p><span style="font-size:x-small;"> </span></p>
<div id="_mcePaste" class="mcePaste" style="position:absolute;width:1px;height:1px;overflow:hidden;top:0;left:-10000px;">﻿</div>
<p>I tried to input the slide ids 19,20,21,22,23 and the output of the CAML query is like below:</p>
<p>&lt;Where&gt;<br />
  &lt;Or&gt;<br />
   &lt;Or&gt;<br />
    &lt;Or&gt;<br />
     &lt;Or&gt;<br />
      &lt;Eq&gt;<br />
        &lt;FieldRef Name=&#8221;ID&#8221; /&gt;<br />
        &lt;Value Type=&#8221;Counter&#8221;&gt;19&lt;/Value&gt;<br />
       &lt;/Eq&gt;<br />
      &lt;Eq&gt;<br />
        &lt;FieldRef Name=&#8221;ID&#8221; /&gt;<br />
        &lt;Value Type=&#8221;Counter&#8221;&gt;20&lt;/Value&gt;<br />
       &lt;/Eq&gt;<br />
     &lt;/Or&gt;<br />
      &lt;Eq&gt;<br />
       &lt;FieldRef Name=&#8221;ID&#8221; /&gt;<br />
       &lt;Value Type=&#8221;Counter&#8221;&gt;21&lt;/Value&gt;<br />
      &lt;/Eq&gt;<br />
     &lt;/Or&gt;<br />
     &lt;Eq&gt;<br />
      &lt;FieldRef Name=&#8221;ID&#8221; /&gt;<br />
      &lt;Value Type=&#8221;Counter&#8221;&gt;22&lt;/Value&gt;<br />
     &lt;/Eq&gt;<br />
    &lt;/Or&gt;<br />
    &lt;Eq&gt;<br />
     &lt;FieldRef Name=&#8221;ID&#8221; /&gt;<br />
     &lt;Value Type=&#8221;Counter&#8221;&gt;23&lt;/Value&gt;<br />
    &lt;/Eq&gt;<br />
   &lt;/Or&gt;<br />
&lt;/Where&gt;</p>
<p>I hope you can understand the utility of the script now. You can now extend it as you like.</p>
<p>Happy coding!</p>
<br />Filed under: <a href='http://usefulscripts.wordpress.com/category/basuc-scripts/'>Basic Scripts</a>, <a href='http://usefulscripts.wordpress.com/category/c-sharp/'>C Sharp</a>, <a href='http://usefulscripts.wordpress.com/category/c-script/'>C# Script</a>, <a href='http://usefulscripts.wordpress.com/category/microsoft-track/'>Microsoft Track</a>, <a href='http://usefulscripts.wordpress.com/category/scripts/'>Scripts</a>, <a href='http://usefulscripts.wordpress.com/category/sharepoint/'>SharePoint</a> Tagged: <a href='http://usefulscripts.wordpress.com/tag/caml/'>CAML</a>, <a href='http://usefulscripts.wordpress.com/tag/getlistitems/'>GetListItems</a>, <a href='http://usefulscripts.wordpress.com/tag/list/'>List</a>, <a href='http://usefulscripts.wordpress.com/tag/query-sharepoint-list/'>Query SharePoint List</a>, <a href='http://usefulscripts.wordpress.com/tag/sharepoint/'>SharePoint</a>, <a href='http://usefulscripts.wordpress.com/tag/view/'>View</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/usefulscripts.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/usefulscripts.wordpress.com/89/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/usefulscripts.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/usefulscripts.wordpress.com/89/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/usefulscripts.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/usefulscripts.wordpress.com/89/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/usefulscripts.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/usefulscripts.wordpress.com/89/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/usefulscripts.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/usefulscripts.wordpress.com/89/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/usefulscripts.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/usefulscripts.wordpress.com/89/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/usefulscripts.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/usefulscripts.wordpress.com/89/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usefulscripts.wordpress.com&amp;blog=1889208&amp;post=89&amp;subd=usefulscripts&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://usefulscripts.wordpress.com/2010/12/22/how-to-write-a-caml-query-to-find-out-a-list-of-items-with-id/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/95e9ce7eea29d07432c0186177ff983a?s=96&#38;d=wavatar&#38;r=G" medium="image">
			<media:title type="html">ubk</media:title>
		</media:content>
	</item>
		<item>
		<title>Difference Between two dates in Javascript</title>
		<link>http://usefulscripts.wordpress.com/2008/10/28/difference-between-two-dats-javascript/</link>
		<comments>http://usefulscripts.wordpress.com/2008/10/28/difference-between-two-dats-javascript/#comments</comments>
		<pubDate>Tue, 28 Oct 2008 10:13:59 +0000</pubDate>
		<dc:creator>ubk</dc:creator>
				<category><![CDATA[Basic Scripts]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[check date]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[date difference]]></category>
		<category><![CDATA[getTime]]></category>
		<category><![CDATA[parsing]]></category>
		<category><![CDATA[setTime]]></category>

		<guid isPermaLink="false">http://usefulscripts.wordpress.com/?p=85</guid>
		<description><![CDATA[Hello. Some times we came around a problem where we needed to know the difference between 2 dates in javascript. Also, in some cases we need to validate a date against another date. In these cases we search in the internet for a suitable function which takes 2 dates as arguments and finally let us [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usefulscripts.wordpress.com&amp;blog=1889208&amp;post=85&amp;subd=usefulscripts&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Hello.</p>
<p>Some times we came around a problem where we needed to know the difference between 2 dates in javascript. Also, in some cases we need to validate a date against another date. In these cases we search in the internet for a suitable function which takes 2 dates as arguments and finally let us know the difference between those.</p>
<p>Here, I have found so many scripts, but some of which are not working and some I do not understand and some does not provide us a simple result as we want. So, I have taken reference of those scripts and marged them into a suitable one to produce a new script which is giving us output as I wanted it to do.</p>
<p>Here it is:</p>
<p>First, we need a html form and 2 input type text fileds to enter 2 dates. Here some may wanted to add the time also.</p>
<p>&lt;form onSubmit=&#8221;return checkDate(this);&#8221;&gt;<br />
&lt;table&gt;<br />
 &lt;tr&gt;<br />
  &lt;td&gt;<br />
   &lt;pre&gt;<br />
   First Date:&lt;input type=&#8221;text&#8221; name=&#8221;firstdate&#8221; value=&#8221;" size=10 maxlength=10&gt; (MM/DD/YYYY format)&lt;br/&gt;<br />
   Time: &lt;input type=&#8221;text&#8221; name=&#8221;firsttime&#8221; value=&#8221;" size=10 maxlength=10&gt; (HH:MM:SS format)&lt;br/&gt;<br />
   <br />
   Second Date: Date: &lt;input type=&#8221;text&#8221; name=&#8221;seconddate&#8221; value=&#8221;" size=10 maxlength=10&gt; (MM/DD/YYYY format)&lt;br/&gt;<br />
   Time: &lt;input type=&#8221;text&#8221; name=&#8221;secondtime&#8221; value=&#8221;" size=10 maxlength=10&gt; (HH:MM:SS format)&lt;br/&gt;<br />
   <br />
   &lt;center&gt;&lt;input type=&#8221;submit&#8221; value=&#8221;Get Difference!&#8221;&gt;&lt;/center&gt;<br />
   &lt;/pre&gt;<br />
  &lt;/td&gt;<br />
 &lt;/tr&gt;<br />
&lt;/table&gt;<br />
&lt;/form&gt;</p>
<p>So, this form has 2 text fileds for dates and 2 other for times, and a button to get the difference.</p>
<p>This form takes date in &#8220;MM/DD/YYYY&#8221; format and time in &#8220;HH:MM:SS&#8221; format.</p>
<p>We can add a function to validate these dates or we can simply specify the date text fileds as readonly and add a suitable javascript calendar to input correct dates. (Like I have used in : <a href="http://www.hotelsagorika.com/reservation.html">http://www.hotelsagorika.com/reservation.html</a>)</p>
<p>Now after clicking the submit button we need to add suitable code to check the dates:</p>
<p>So, we will put this script into the HEAD section of the HTML.</p>
<p>&lt;script type=&#8221;text/javascript&#8221;&gt;</p>
<p>function isValidDate(dateStr) {<br />
 // Date validation Function <br />
 // Checks For the following valid Date formats:<br />
 // MM/DD/YY MM/DD/YYYY MM-DD-YY MM-DD-YYYY<br />
 <br />
 var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit Year<br />
 <br />
 var matchArray = dateStr.match(datePat); // Is the format ok?<br />
 if (matchArray == Null) {<br />
  alert(dateStr + &#8221; Date is not in a valid format.&#8221;)<br />
  return false;<br />
 }<br />
 <br />
 Month = matchArray[1]; // parse Date into variables<br />
 Day = matchArray[3];<br />
 Year = matchArray[4];<br />
 <br />
 if (Month &lt; 1 || Month &gt; 12) { // check Month range<br />
  alert(&#8220;Month must be between 1 and 12.&#8221;);<br />
  return false;<br />
 }<br />
 <br />
 if (Day &lt; 1 || Day &gt; 31) {<br />
  alert(&#8220;Day must be between 1 and 31.&#8221;);<br />
  return false;<br />
 }<br />
 <br />
 if ((Month==4 || Month==6 || Month==9 || Month==11) &amp;&amp; Day==31) {<br />
  alert(&#8220;Month &#8220;+Month+&#8221; doesn&#8217;t have 31 days!&#8221;)<br />
  return false;<br />
 }<br />
 <br />
 if (Month == 2) { // check For february 29th<br />
  var isleap = (Year % 4 == 0 &amp;&amp; (Year % 100 != 0 || Year % 400 == 0));<br />
  if (Day&gt;29 || (Day==29 &amp;&amp; !isleap)) {<br />
   alert(&#8220;February &#8221; + Year + &#8221; doesn&#8217;t have &#8221; + Day + &#8221; days!&#8221;);<br />
   return false;<br />
  }<br />
 }<br />
 return true;<br />
}</p>
<p>function isValidTime(timeStr) {<br />
 // Checks if time Is In HH:MM:SS AM/PM format.<br />
 // The seconds And AM/PM are optional.<br />
 <br />
 var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;<br />
 <br />
 var matchArray = timeStr.match(timePat);<br />
 if (matchArray == Null) {<br />
  alert(&#8220;Time is not in a valid format.&#8221;);<br />
  return false;<br />
 }<br />
 <br />
 Hour = matchArray[1];<br />
 Minute = matchArray[2];<br />
 Second = matchArray[4];<br />
 ampm = matchArray[6];<br />
 <br />
 if (Second==&#8221;") { Second = null; }<br />
 if (ampm==&#8221;") { ampm = Null }<br />
 <br />
 if (Hour &lt; 0 || Hour &gt; 23) {<br />
  alert(&#8220;Hour must be between 1 and 12. (or 0 and 23 for military time)&#8221;);<br />
  return false;<br />
 }<br />
 if (Hour &lt;= 12 &amp;&amp; ampm == Null) {<br />
  if (confirm(&#8220;Please indicate which time format you are using. OK = Standard Time, CANCEL = Military Time&#8221;)) {<br />
   alert(&#8220;You must specify AM or PM.&#8221;);<br />
   return false;<br />
  }<br />
 }<br />
 if (Hour &gt; 12 &amp;&amp; ampm != Null) {<br />
  alert(&#8220;You can&#8217;t specify AM or PM for military time.&#8221;);<br />
  return false;<br />
 }<br />
 if (Minute &lt; 0 || Minute &gt; 59) {<br />
  alert (&#8220;Minute must be between 0 and 59.&#8221;);<br />
  return false;<br />
 }<br />
 if (Second != Null &amp;&amp; (Second &lt; 0 || Second &gt; 59)) {<br />
  alert (&#8220;Second must be between 0 and 59.&#8221;);<br />
  return false;<br />
 }<br />
 return true;<br />
}</p>
<p>function checkDate(dateform) {<br />
 date1 = new Date();<br />
 date2 = new Date();<br />
 diff = new Date();<br />
 <br />
 if (isValidDate(dateform.firstdate.value) &amp;&amp; isValidTime(dateform.firsttime.value)) { // Validates first Date<br />
  date1temp = new Date(dateform.firstdate.value + &#8221; &#8221; + dateform.firsttime.value);<br />
  date1.setTime(date1temp.getTime());<br />
 }<br />
 else return false; // otherwise exits<br />
 <br />
 if (isValidDate(dateform.seconddate.value) &amp;&amp; isValidTime(dateform.secondtime.value)) { // Validates Second Date<br />
  date2temp = new Date(dateform.seconddate.value + &#8221; &#8221; + dateform.secondtime.value);<br />
  date2.setTime(date2temp.getTime());<br />
 }<br />
 else return false; // otherwise exits<br />
 <br />
 // sets difference Date To difference of first Date And Second Date<br />
 <br />
 diff.setTime(Math.abs(date1.getTime() &#8211; date2.getTime()));<br />
 <br />
 timediff = diff.getTime();<br />
 <br />
 weeks = Math.floor(timediff / (1000 * 60 * 60 * 24 * 7));<br />
 timediff -= weeks * (1000 * 60 * 60 * 24 * 7);<br />
 <br />
 days = Math.floor(timediff / (1000 * 60 * 60 * 24));<br />
 timediff -= days * (1000 * 60 * 60 * 24);<br />
 <br />
 hours = Math.floor(timediff / (1000 * 60 * 60));<br />
 timediff -= hours * (1000 * 60 * 60);<br />
 <br />
 mins = Math.floor(timediff / (1000 * 60));<br />
 timediff -= mins * (1000 * 60);<br />
 <br />
 secs = Math.floor(timediff / 1000);<br />
 timediff -= secs * 1000;<br />
 <br />
 alert(&#8220;Difference = &#8221; + weeks + &#8221; weeks, &#8221; + days + &#8221; days, &#8221; + hours + &#8221; hours, &#8221; + mins + &#8221; minutes, and &#8221; + secs + &#8221; seconds&#8221;);</p>
<p> return false; // form should never submit, returns False<br />
}</p>
<p>&lt;/script&gt;</p>
<p>If you have specified the text fileds as read only and added a calendar, then you do not need the validattion methods, then the script will look like:</p>
<p>&lt;script type=&#8221;text/javascript&#8221;&gt;</p>
<p>function checkDate(dateform) {<br />
 date1 = new Date();<br />
 date2 = new Date();<br />
 diff = new Date();<br />
 <br />
 date1temp = new Date(dateform.firstdate.value + &#8221; &#8221; + dateform.firsttime.value);</p>
<p>//If you do not need the time then you can replace the above line with this<br />
// date1temp = new Date(dateform.firstdate.value + &#8221; 00:00:00&#8243;);</p>
<p> date1.setTime(date1temp.getTime());<br />
  <br />
 date2temp = new Date(dateform.seconddate.value + &#8221; &#8221; + dateform.secondtime.value);</p>
<p>//If you do not need the time then you can replace the above line with this<br />
// date2temp = new Date(dateform.seconddate.value + &#8221; 00:00:00&#8243;);</p>
<p> date2.setTime(date2temp.getTime());<br />
 <br />
 // sets difference Date To difference of first Date And Second Date<br />
 <br />
 diff.setTime(Math.abs(date1.getTime() &#8211; date2.getTime()));<br />
 <br />
 timediff = diff.getTime();<br />
 <br />
 weeks = Math.floor(timediff / (1000 * 60 * 60 * 24 * 7));<br />
 timediff -= weeks * (1000 * 60 * 60 * 24 * 7);<br />
 <br />
 days = Math.floor(timediff / (1000 * 60 * 60 * 24));<br />
 timediff -= days * (1000 * 60 * 60 * 24);<br />
 <br />
 hours = Math.floor(timediff / (1000 * 60 * 60));<br />
 timediff -= hours * (1000 * 60 * 60);<br />
 <br />
 mins = Math.floor(timediff / (1000 * 60));<br />
 timediff -= mins * (1000 * 60);<br />
 <br />
 secs = Math.floor(timediff / 1000);<br />
 timediff -= secs * 1000;<br />
 <br />
 alert(&#8220;Difference = &#8221; + weeks + &#8221; weeks, &#8221; + days + &#8221; days, &#8221; + hours + &#8221; hours, &#8221; + mins + &#8221; minutes, and &#8221; + secs + &#8221; seconds&#8221;);</p>
<p> return false; // form should never submit, returns False<br />
}</p>
<p>&lt;/script&gt;</p>
<p>Now, suppose we want to know if the second date is after the first date or vice versa, i.e. if the first date is 30 th October, 2008 and seconda date is 02 nd November, 2008 then it will accept the dates otherwise, like if the 1st date is 28 th October 2008 and the second is 25th October 2008, it will not accept.</p>
<p>So, wee need to modify the above function slightly:</p>
<p>function checkDate(dateform) {<br />
 date1 = new Date();<br />
 date2 = new Date();<br />
 diff = new Date();<br />
 <br />
 if (isValidDate(dateform.firstdate.value) &amp;&amp; isValidTime(dateform.firsttime.value)) { // Validates first Date<br />
  date1temp = new Date(dateform.firstdate.value + &#8221; &#8221; + dateform.firsttime.value);<br />
  date1.setTime(date1temp.getTime());<br />
 }<br />
 else return false; // otherwise exits<br />
 <br />
 if (isValidDate(dateform.seconddate.value) &amp;&amp; isValidTime(dateform.secondtime.value)) { // Validates Second Date<br />
  date2temp = new Date(dateform.seconddate.value + &#8221; &#8221; + dateform.secondtime.value);<br />
  date2.setTime(date2temp.getTime());<br />
 }<br />
 else return false; // otherwise exits<br />
 <br />
 // sets difference Date To difference of first Date And Second Date<br />
 <br />
 var dif = date2-date1;<br />
 <br />
 if(dif &gt;=0){ // 2nd date is after the 1st date<br />
  return true;<br />
 }else{<br />
  return false;<br />
 }</p>
<p>}</p>
<p>Hope this article will help you understanding the date related issues better.</p>
<br />Posted in Basic Scripts, javascript, Scripts Tagged: check date, date, date difference, getTime, javascript, parsing, setTime <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/usefulscripts.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/usefulscripts.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/usefulscripts.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/usefulscripts.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/usefulscripts.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/usefulscripts.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/usefulscripts.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/usefulscripts.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/usefulscripts.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/usefulscripts.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/usefulscripts.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/usefulscripts.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/usefulscripts.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/usefulscripts.wordpress.com/85/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usefulscripts.wordpress.com&amp;blog=1889208&amp;post=85&amp;subd=usefulscripts&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://usefulscripts.wordpress.com/2008/10/28/difference-between-two-dats-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/95e9ce7eea29d07432c0186177ff983a?s=96&#38;d=wavatar&#38;r=G" medium="image">
			<media:title type="html">ubk</media:title>
		</media:content>
	</item>
		<item>
		<title>Integer Parsing in JavaScript</title>
		<link>http://usefulscripts.wordpress.com/2008/10/02/integer-parsing-in-javascript/</link>
		<comments>http://usefulscripts.wordpress.com/2008/10/02/integer-parsing-in-javascript/#comments</comments>
		<pubDate>Thu, 02 Oct 2008 11:48:41 +0000</pubDate>
		<dc:creator>ubk</dc:creator>
				<category><![CDATA[Basic Scripts]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[digit]]></category>
		<category><![CDATA[integer]]></category>
		<category><![CDATA[isdigit]]></category>
		<category><![CDATA[isempty]]></category>
		<category><![CDATA[isinteger]]></category>
		<category><![CDATA[isNaN]]></category>
		<category><![CDATA[number]]></category>
		<category><![CDATA[parse]]></category>
		<category><![CDATA[parse integer]]></category>
		<category><![CDATA[parseInt]]></category>
		<category><![CDATA[parsing]]></category>
		<category><![CDATA[tryparse]]></category>
		<category><![CDATA[undefined]]></category>

		<guid isPermaLink="false">http://usefulscripts.wordpress.com/?p=80</guid>
		<description><![CDATA[Integer Parsing in JavaScript. Some utility methods and tips First Question will be like: How do I convert strings to numbers in JavaScript? Answer: To convert a string to a number, use the JavaScript function parseFloat (for conversion to a floating-point number) or parseInt (for conversion to an integer). parseFloat syntax: parseFloat('string') How it works: [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usefulscripts.wordpress.com&amp;blog=1889208&amp;post=80&amp;subd=usefulscripts&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h1>Integer Parsing in JavaScript. Some utility methods and tips</h1>
<p class="MsoNormal"><strong><span style="font-family:Verdana;">First Question will be like:</span></strong><span style="font-family:Verdana;"> How do I convert strings to numbers in JavaScript?</span></p>
<p><strong><span style="font-family:Verdana;">Answer:</span></strong><span style="font-family:Verdana;"> To convert a string to a number, use the JavaScript function </span><code><span style="text-decoration:underline;"><span style="color:#0000ff;font-family:Verdana;">parseFloat</span></span></code><span style="font-family:Verdana;"> (for conversion to a floating-point number) or </span><code><span style="text-decoration:underline;"><span style="color:#0000ff;font-family:Verdana;">parseInt</span></span></code><span style="font-family:Verdana;"> (for conversion to an integer). </span></p>
<p><strong><em><span style="font-family:Verdana;">parseFloat</span></em></strong><em><span style="font-family:Verdana;"> syntax:</span></em><span style="font-family:Verdana;"> </span><code><span style="font-family:Verdana;">parseFloat('string')</span></code><span style="font-family:Verdana;"> </span></p>
<p style="line-height:125%;"><strong><em><span style="font-family:Verdana;">How it works:<br />
</span></em></strong><span style="font-family:Verdana;"><br />
The argument of </span><code><span style="font-family:Verdana;">parseFloat</span></code><span style="font-family:Verdana;"> must be a string or a string expression. The result of </span><code><span style="font-family:Verdana;">parseFloat</span></code><span style="font-family:Verdana;"> is the number whose decimal representation was contained in that string (or the number found in the beginning of the string). If the string argument cannot be parsed as a decimal number, the results will be different in different browsers (either </span><code><span style="font-family:Verdana;">0</span></code><span style="font-family:Verdana;"> or </span><code><strong><span style="font-family:Verdana;">NaN</span></strong></code><span style="font-family:Verdana;">). </span></p>
<p><em><span style="font-family:Verdana;">Examples</span></em><span style="font-family:Verdana;"> (comments in each line give the conversion results): </span></p>
<p><span style="font-size:12pt;font-family:Verdana;"><strong>parseFloat(&#8217;1.45kg&#8217;)</strong><span>  </span>// 1.45</span><br />
<span style="font-size:12pt;font-family:Verdana;"><strong>parseFloat(&#8217;77.3&#8242;)</strong><span>    </span>// 77.3</span><br />
<span style="font-size:12pt;font-family:Verdana;"><strong>parseFloat(&#8217;077.3&#8242;)</strong><span>   </span>// 77.3</span><br />
<span style="font-size:12pt;font-family:Verdana;"><strong>parseFloat(&#8217;0&#215;77.3&#8242;)</strong><span>  </span>// 0</span><br />
<span style="font-size:12pt;font-family:Verdana;"><strong>parseFloat(&#8216;.3&#8242;)</strong><span>      </span>// 0.3</span><br />
<span style="font-size:12pt;font-family:Verdana;"><strong>parseFloat(&#8217;0.1e6&#8242;)</strong><span>   </span>// 100000 </span>
</p>
<p style="margin:0 0 .0001pt;"><strong><em><span style="font-family:Verdana;">parseInt</span></em></strong><em><span style="font-family:Verdana;"> syntax:</span></em><span style="font-family:Verdana;"> </span><code><span style="font-family:Verdana;">parseInt( 'string' [, base] ) </span></code></p>
<p style="margin:0 0 .0001pt;"><strong><em><span style="font-family:Verdana;">How it works:</span></em></strong></p>
<p style="line-height:125%;margin:0 0 .0001pt;"><span style="font-family:Verdana;">The first argument of </span><code><span style="font-family:Verdana;">parseInt</span></code><span style="font-family:Verdana;"> must be a string or a string expression. The result returned by </span><code><span style="font-family:Verdana;">parseInt</span></code><span style="font-family:Verdana;"> is an <em>integer</em> whose representation was contained in that string (or the integer found in the beginning of the string). The second argument (</span><code><span style="font-family:Verdana;">base</span></code><span style="font-family:Verdana;">), if present, specifies the base (radix) of the number whose string representation is contained in the </span><code><span style="font-family:Verdana;">string</span></code><span style="font-family:Verdana;">. The </span><code><span style="font-family:Verdana;">base</span></code><span style="font-family:Verdana;"> argument can be any integer from 2 to 36. </span></p>
<p style="line-height:125%;margin:0 0 .0001pt;"> </p>
<p style="line-height:125%;margin:0 0 .0001pt;"><span style="font-family:Verdana;">If there is only one argument, the number base is detected according to the general JavaScript syntax for numbers. Strings that begin with </span><code><span style="font-family:Verdana;">0x</span></code><span style="font-family:Verdana;"> or </span><code><span style="font-family:Verdana;">-0x</span></code><span style="font-family:Verdana;"> are parsed as hexadecimals; strings that begin with </span><code><span style="font-family:Verdana;">0</span></code><span style="font-family:Verdana;"> or </span><code><span style="font-family:Verdana;">-0</span></code><span style="font-family:Verdana;"> are parsed as octal numbers. All other strings are parsed as decimal numbers.</span></p>
<p style="line-height:125%;margin:0 0 .0001pt;"><span style="font-family:Verdana;">If the string argument cannot be parsed as an integer, the results will be different in different browsers (either </span><code><span style="font-family:Verdana;">0</span></code><span style="font-family:Verdana;"> or </span><code><span style="font-family:Verdana;">NaN</span></code><span style="font-family:Verdana;">). </span></p>
<p style="line-height:125%;margin:0 0 .0001pt;"> </p>
<p style="line-height:125%;margin:0 0 .0001pt;"><em><span style="font-family:Verdana;">Examples</span></em><span style="font-family:Verdana;"> (comments in each line give the conversion results): </span></p>
<p style="line-height:125%;margin:0 0 .0001pt;"><span style="font-size:12pt;font-family:Verdana;">parseInt(&#8217;123.45&#8242;)<span>  </span>// 123</span><br />
<span style="font-size:12pt;font-family:Verdana;">parseInt(&#8217;77&#8242;)<span>      </span>// 77</span><br />
<span style="font-size:12pt;font-family:Verdana;">parseInt(&#8217;077&#8242;,10)<span>  </span>// 77</span><br />
<span style="font-size:12pt;font-family:Verdana;">parseInt(&#8217;77&#8242;,8)<span>    </span>// 63<span>  </span>(= 7 + 7*8)</span><br />
<span style="font-size:12pt;font-family:Verdana;">parseInt(&#8217;077&#8242;)<span>     </span>// 63<span>  </span>(= 7 + 7*8)</span><br />
<span style="font-size:12pt;font-family:Verdana;">parseInt(&#8217;77&#8242;,16)<span>   </span>// 119 (= 7 + 7*16)</span><br />
<span style="font-size:12pt;font-family:Verdana;">parseInt(&#8217;0&#215;77&#8242;)<span>    </span>// 119 (= 7 + 7*16)</span><br />
<span style="font-size:12pt;font-family:Verdana;">parseInt(&#8217;099&#8242;)<span>     </span>// 0 (9 is not an octal digit)</span><br />
<span style="font-size:12pt;font-family:Verdana;">parseInt(&#8217;99&#8242;,8)<span>    </span>// 0 or NaN, depending on the platform</span><br />
<span style="font-size:12pt;font-family:Verdana;">parseInt(&#8217;0.1e6&#8242;)<span>   </span>// 0</span><br />
<span style="font-size:12pt;font-family:Verdana;">parseInt(&#8216;ZZ&#8217;,36)<span>   </span>// 1295 (= 35 + 35*36)</span>
</p>
<p class="MsoNormal"><span style="font-family:Verdana;">So, if you try with “08” or “09” in <strong>parseInt(“value”)</strong>, you may notice a strange thing: it treats &#8220;08&#8243; or &#8220;09&#8243; as 0. </span></p>
<p class="MsoNormal"><span style="font-family:Verdana;">This happens for all invalid inputs. The answer to this question lies in above examples. </span></p>
<p class="MsoNormal" style="line-height:125%;"><span style="font-family:Verdana;">An integer can be represented in three different ways: in base 8 (octal), base 10 (decimal) and base 16 (hexadecimal). A starting zero indicates an octal number, which can be anything from 0 to 7 but not 8 or 9. Similarly, a starting “0x” followed by a number that can indicates a hexadecimal number which can be anything from 0 to F.</span></p>
<p>The solution to the problem is very simple. Since &#8220;08&#8243; means an octal number because of a starting 0, parseInt returns a zero for 8. Now, parseInt accepts a second optional parameter, which is the base of the number. You can forcefully tell it to treat the number as a decimal one by sending a second parameter 10.</p>
<p class="MsoNormal"><strong><span style="font-family:Verdana;">Var _num = “08”; //or you can take input from a Form.</span></strong></p>
<p class="MsoNormal"><strong><span style="font-family:Verdana;">var theNumber = parseInt (_num, 10);</span></strong></p>
<p class="MsoNormal"><strong><span style="text-decoration:underline;"><span style="font-size:14pt;font-family:Verdana;">A utility method to check if the value is integer or not</span></span></strong><strong><span style="font-family:Verdana;"><br />
[like </span></strong><em><span style="font-family:Verdana;">int.TryParse </span></em><em><span style="font-style:normal;font-family:Verdana;">Method in .NET]</span></em>
</p>
<p class="MsoNormal"><span style="font-family:Verdana;">It makes it really convenient when evaluating a string value as an <span class="hilite1">integer</span>. But, </span><span class="hilite2"><span style="font-family:Verdana;">JavaScript</span></span><span style="font-family:Verdana;"> has no equivalent so you need to do your own evaluation every time.</span></p>
<p class="MsoNormal"><span style="font-family:Verdana;">Here&#8217;s a simple <span class="hilite2">JavaScript</span> method, which I found in the Internet:</span></p>
<table style="border:medium none;border-collapse:collapse;" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="width:6.15in;border:windowtext .5pt solid;padding:0 5.4pt;" width="590" valign="top">
<p class="MsoNormal"><em><span style="font-family:Verdana;">function <strong>TryParseInt</strong>(str,defaultValue){</span></em></p>
<p class="MsoNormal"><em><span style="font-family:Verdana;">var retValue = defaultValue;</span></em></p>
<p class="MsoNormal"><em><span style="font-family:Verdana;">if(typeof str != &#8216;undefined&#8217; &amp;&amp; str!=null &amp;&amp; str.length&gt;0){</span></em></p>
<p class="MsoNormal"><em><span style="font-family:Verdana;">        if (!isNaN(str)){</span></em></p>
<p class="MsoNormal"><em><span style="font-family:Verdana;">               retValue = parseInt(str);</span></em></p>
<p class="MsoNormal"><em><span style="font-family:Verdana;">         }<span> </span></span></em></p>
<p class="MsoNormal"><em><span style="font-family:Verdana;">}</span></em></p>
<p class="MsoNormal"><em><span style="font-family:Verdana;">return retValue;</span></em></p>
<p class="MsoNormal"><em><span style="font-family:Verdana;">}</span></em></p>
</td>
</tr>
</tbody>
</table>
<p style="margin:0 0 .0001pt;"><span style="font-family:Verdana;">The first parameter of the TryParseInt method is the string you want to evaluate, and the second parameter is the default value to return if the string cannot be evaluated to an <span class="hilite1">integer</span>.</span></p>
<p><span style="font-family:Verdana;">Here are some example usages:</span></p>
<table style="border:medium none;border-collapse:collapse;" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="width:6.15in;border:windowtext .5pt solid;padding:0 5.4pt;" width="590" valign="top">
<p class="MsoNormal"><span style="font-family:Verdana;">//This will return 7</span></p>
<p class="MsoNormal"><span style="font-family:Verdana;">var a = TryParseInt(&#8220;7&#8243;, 0);</span></p>
<p class="MsoNormal"><span style="font-family:Verdana;">alert(a);</span></p>
<p class="MsoNormal"><span style="font-family:Verdana;">//This will return 0</span></p>
<p class="MsoNormal"><span style="font-family:Verdana;">var b = TryParseInt(&#8220;6c&#8221;, 0);</span></p>
<p class="MsoNormal"><span style="font-family:Verdana;">alert(b);</span></p>
<p class="MsoNormal"><span style="font-family:Verdana;">//This will return null</span></p>
<p class="MsoNormal"><span style="font-family:Verdana;">var c = TryParseInt(&#8220;abcd&#8221;, null);</span></p>
<p class="MsoNormal"><span style="font-family:Verdana;">alert(c);</span></p>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal"><strong><span style="text-decoration:underline;"><span style="font-family:Verdana;">An alternative to this above TryParseInt method will be :</span></span></strong></p>
<table style="border:medium none;border-collapse:collapse;" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="width:6.15in;border:windowtext .5pt solid;padding:0 5.4pt;" width="590" valign="top"><strong><span style="font-size:12pt;font-family:Verdana;">// Returns true if the argument is a valid Integer, </span></strong></p>
<p><strong><span style="font-size:12pt;font-family:Verdana;">//else returns false.<br />
</span></strong></p>
<p><span style="font-size:12pt;font-family:Verdana;">function <strong>isInteger</strong> (value) </span><span style="font-size:12pt;font-family:Verdana;"><span>   </span>{</span> <span style="font-size:12pt;font-family:Verdana;"><span>     <br />
</span><br />
var i;</span> <span style="font-size:12pt;font-family:Verdana;"><span><br />
</span><br />
if (isEmpty(value)) return false;</span> </p>
<p><span style="font-size:12pt;font-family:Verdana;">for (i = 0; i &lt; value.length; i++)</span> <span style="font-size:12pt;font-family:Verdana;"><span>     <br />
</span>{</span><br />
<span style="font-size:12pt;font-family:Verdana;"><span>         </span>var c = value.charAt(i);</span> <br />
<span style="font-size:12pt;font-family:Verdana;"><span>         </span>if (false == isDigit(c))<br />
                  return false;</span> <span style="font-size:12pt;font-family:Verdana;"><span>     <br />
</span></span><span style="font-size:12pt;font-family:Verdana;"> }</span><br />
<span style="font-size:12pt;font-family:Verdana;"> return true;</span> <span style="font-size:12pt;font-family:Verdana;"><span>  <br />
</span>}</span> </p>
<p> <strong><span style="font-size:12pt;font-family:Verdana;">//Checks if the input string is empty or not</span></strong></p>
<p><span style="font-size:12pt;font-family:Verdana;">function <strong>isEmpty</strong>(value)</span> <span style="font-size:12pt;font-family:Verdana;"><span>  <br />
</span>{</span> <span style="font-size:12pt;font-family:Verdana;"><span>     <br />
</span></span><span style="font-size:12pt;font-family:Verdana;">     return ((typeof value == &#8216;undefined&#8217;) || (value == null) || (value.length == 0));</span> <span style="font-size:12pt;font-family:Verdana;"><span>  <br />
</span>}</span></p>
<p><strong><span style="font-size:12pt;font-family:Verdana;">//Checks if the input character is a digit or not</span></strong> </p>
<p><span style="font-size:12pt;font-family:Verdana;">function <strong>isDigit</strong> (c)</span> <span style="font-size:12pt;font-family:Verdana;"><span>   </span>{</span> <span style="font-size:12pt;font-family:Verdana;"><span>     <br />
</span>     return ((c &gt;= &#8220;0&#8243;) &amp;&amp; (c &lt;= &#8220;9&#8243;));</span> <span style="font-size:12pt;font-family:Verdana;"><span>  </span></span></p>
<p><span style="font-size:12pt;font-family:Verdana;"><span> </span>}</span></td>
</tr>
</tbody>
</table>
<p class="MsoNormal"><span style="font-size:12pt;font-family:Verdana;">Hope this article helps you [:D]</span></p>
<br />Posted in Basic Scripts, javascript, Scripts Tagged: digit, integer, isdigit, isempty, isinteger, isNaN, javascript, number, parse, parse integer, parseInt, parsing, tryparse, undefined <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/usefulscripts.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/usefulscripts.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/usefulscripts.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/usefulscripts.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/usefulscripts.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/usefulscripts.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/usefulscripts.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/usefulscripts.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/usefulscripts.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/usefulscripts.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/usefulscripts.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/usefulscripts.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/usefulscripts.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/usefulscripts.wordpress.com/80/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usefulscripts.wordpress.com&amp;blog=1889208&amp;post=80&amp;subd=usefulscripts&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://usefulscripts.wordpress.com/2008/10/02/integer-parsing-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/95e9ce7eea29d07432c0186177ff983a?s=96&#38;d=wavatar&#38;r=G" medium="image">
			<media:title type="html">ubk</media:title>
		</media:content>
	</item>
	</channel>
</rss>
