Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Date as Input Parameter

Posted on 2009-04-13
9
Medium Priority
?
621 Views
Last Modified: 2013-12-17
Hi,

I am struggling to filter a asp.net c# query with a unix date input parameter, I am trying to filter my query results between todays date and a date 14 days later. I think I am getting mixed up with the format of the date to input into the string as I have tried a lot of diferent options but get format string error messages back when I run the page. Here is my code I have so far, ;

        DateTime DateParameter = DateTime.Today;
        DateTime DateParameter1 = DateTime.Today.AddDays(14);
        double Parameter = Util.ConvertToUnixTimestamp(DateParameter);
        double Parameter1 = Util.ConvertToUnixTimestamp(DateParameter1);
        e.InputParameters["Date"] = Parameter.ToString();
        e.InputParameters["Date1"] = Parameter1.ToString();

I have replaced

        e.InputParameters["Date"] = Parameter.ToString();
        e.InputParameters["Date1"] = Parameter1.ToString();

with

        e.InputParameters["Date"] = "1018310400";
        e.InputParameters["Date1"] = "101952000";

which works perfectly, but when I run the first bit of code no results are displayed.


Thanks


Chris
0
Comment
Question by:chrispaton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 24130162
What data type is e?  There appears to be something in the type conversions that is not as expected ...

Those values you hard-coded to test -- are they EXACTLY what Parameter.ToString() and parameter1.ToString() are returning?
0
 

Author Comment

by:chrispaton
ID: 24130202
This is the select parameter details;

                                                <SelectParameters>
                                                <asp:Parameter Name="Date" Type="String" />
                                                </SelectParameters>
                                                <SelectParameters>
                                                <asp:Parameter Name="Date1" Type="String" />
                                                </SelectParameters>

The values hard coded are supposed to be what the parameter and parameter1 should be returning but are not, I added the hard coded values to make sure that I was parsing something to the page and that my query was correct.
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 24130282
>>The values hard coded are supposed to be what the parameter and parameter1 should be returning but are not.

So  Util.ConvertToUnixTimestamp is not performing properly.

Can you post its code?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:chrispaton
ID: 24130512
public class Util
{
    /// <summary>
    /// Convert a date time to unix timestamp
    /// </summary>
    /// <param name="date"></param>
    /// <returns></returns>
    public static double ConvertToUnixTimestamp(DateTime date)
    {
        DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
        TimeSpan diff = date - origin;
        return Math.Floor(diff.TotalSeconds);
    }

    /// <summary>
    /// Convert a unix timestamp to a date time
    /// </summary>
    /// <param name="timestamp"></param>
    /// <returns></returns>
    public static DateTime ConvertFromUnixTimestamp(double timestamp)
    {
        DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
        return origin.AddSeconds(timestamp);
    }
}


0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 24130688
What happens if you change the math.floor call to an explicit (long) cast?  And return a long?

http://andrewgunn.blogspot.com/2008/07/how-do-i-convert-unix-timestamp-into.html
0
 

Author Comment

by:chrispaton
ID: 24137247
I have changed the math.floor call to (long) and return a long as you suggested but it's still not returning anything.
Please help

0
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 2000 total points
ID: 24137566
Not returning anything at all?

I've gone back to your code & put it into a quick test app -- which works.

Can you trace the app?  Are your dates being set as expected?  And the doubles called Parameter and Parameter1 -- what values do they have immediately after the call?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace UnixTime
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = util.ConvertToUnixTimestamp(dateTimePicker1.Value).ToString();
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            label1.Text = util.ConvertToUnixTimestamp(DateTime.Today).ToString();
        }
 
        private void button3_Click(object sender, EventArgs e)
        {
            label1.Text = util.ConvertToUnixTimestamp(DateTime.Today.AddDays(14)).ToString();
        }
    }
 
    public class util
    {
        /// <summary>
        /// Convert a date time to unix timestamp
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static double ConvertToUnixTimestamp(DateTime date)
        {
            DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            TimeSpan diff = date - origin;
            return Math.Floor(diff.TotalSeconds);
        }
 
        /// <summary>
        /// Convert a unix timestamp to a date time
        /// </summary>
        /// <param name="timestamp"></param>
        /// <returns></returns>
        public static DateTime ConvertFromUnixTimestamp(double timestamp)
        {
            DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return origin.AddSeconds(timestamp);
        }
    }
 
}

Open in new window

0
 

Author Comment

by:chrispaton
ID: 24138543
hi daniel,  
thanks for your help.this was the code we used without the doubles and it works.
e.InputParameters["Date"] = Util.ConvertToUnixTimestamp(DateTime.Today).ToString();
e.InputParameters["Date1"] = Util.ConvertToUnixTimestamp(DateTime.Today.AddDays(21)).ToString();
much appreciated
chris

0
 

Author Closing Comment

by:chrispaton
ID: 31569509
hi daniel.
thanks for your help.
this was the code without the doubles and it works
e.InputParameters["Date"] = Util.ConvertToUnixTimestamp(DateTime.Today).ToString();
        e.InputParameters["Date1"] = Util.ConvertToUnixTimestamp(DateTime.Today.AddDays(21)).ToString();
much appreciated
chris
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question