简体   繁体   English

以可以在Azure移动服务(Javascript)中查询的方式保存DateTime

[英]Save DateTime in a way that you can query in Azure Mobile Services (Javascript)

I want to save a DateTime value to an Azure Mobile Services backend (javascript), but if you save it as DateTime you aren't able to query on that date. 我想将DateTime值保存到Azure移动服务后端(javascript),但是如果将其保存为DateTime,则无法在该日期进行查询。 The reason I want to do that is because I want to retrieve all items that are within a certain date range. 我要这样做的原因是,我想检索特定日期范围内的所有项目。 I've tried to save them as ticks, but this also doens't work because the numeric value is to large. 我试图将它们另存为刻度,但这也没有用,因为数值太大。

Does anyone have an idea? 有人有主意吗?

That's not correct - if you save a DateTime (date) value in the JS backend you can query based on that date. 这是不正确的-如果您在JS后端中保存DateTime(日期)值,则可以基于该日期进行查询。 All relational operations ( >=, <=, <, >, ==, != ) work find with date values. 所有关系运算( >=, <=, <, >, ==, != )都可以使用日期值来查找。

For example, in this JS code below (for a service with a table called so ), it can insert 4 dates, and send a range query that would return the two middle ones. 例如,在下面的JS代码中(对于带有表so的服务),它可以插入4个日期,并发送一个范围查询,该查询将返回两个中间的日期。

<html>
  <head>
    <title>Test site</title>
    <script src="http://ajax.aspnetcdn.com/ajax/mobileservices/MobileServices.Web-1.2.5.min.js"></script>
  </head>
  <body>
    <h1>Playing with Azure</h1>
    <button onclick="insertData();">Insert data</button>
    <button onclick="readData();">read data</button>
    <ul id='results'></ul>
    <script type="text/javascript">
        var client = new WindowsAzure.MobileServiceClient(
            "https://SERVICENAME.azure-mobile.net/",
            "APPLICATIONKEY"
        );
        var table = client.getTable("so");
        function handleError(err) {
            addLog('Error: ' + err);
        }
        function addLog(text) {
            var ul = document.getElementById('results');
            var li = document.createElement('li');
            li.appendChild(document.createTextNode(text));
            ul.appendChild(li);
        }
        function insertData() {
            table.insert({ myfield: 12, date: new Date(2014, 11, 1) }).then(function() {
                addLog('Inserted data in December');
                table.insert({ myfield: 11, date: new Date(2014, 10, 1) }).then(function() {
                    addLog('Inserted data in November');
                    table.insert({ myfield: 10, date: new Date(2014, 9, 1) }).then(function() {
                        addLog('Inserted data in October');
                        table.insert({ myfield: 9, date: new Date(2014, 8, 1) }).then(function() {
                            addLog('Inserted data in Setember');
                        }, handleError);
                    }, handleError);
                }, handleError);
            }, handleError);
        }

        function readData() {
            var firstDate = new Date(2014, 8, 15);
            var lastDate = new Date(2014, 10, 15);
            table.where(function(firstDate, lastDate) {
                return this.date >= firstDate && this.date <= lastDate;
            }, firstDate, lastDate).read().done(function(results) {
                addLog('Results.length: ' + results.length);
                for (var i = 0; i < results.length; i++) {
                    addLog('Results[' + i + ']: ' + JSON.stringify(results[i]));
                }
            }, handleError);
        }
    </script>
  </body>
</html>

A similar code can be written for a managed client as well. 也可以为托管客户端编写类似的代码。 Haven't compiled it (written in notepad), but it would look somewhat like the code below: 尚未编译(在记事本中编写),但是看起来有点像下面的代码:

private static MobileServiceClient client = new MobileServiceClient(
    "https://SERVICENAME.azure-mobile.net", "APPLICATION_KEY");
private static IMobileServiceTable<MyType> table = client.GetTable<MyType>();

private async void InsertData_Click(object sender, EventArgs args) {
    for (int month = 12; month >= 9; month--) {
        var date = new DateTime(2014, month, 1, 0, 0, 0, DateTimeKind.UTC);
        await table.InsertAsync(new MyType { date = date, myfield = month });
    }
}

private async void ReadData_Click(object sender, EventArgs args) {
    var firstDate = new DateTime(2014, 9, 15, 0, 0, 0, DateTimeKind.UTC);
    var lastDate = new DateTime(2014, 11, 15, 0, 0, 0, DateTimeKind.UTC);
    var items = await table
        .Where(t => t.date >= firstTime && t.date <= lastTime)
        .ToListAsync();
    foreach (var item in items) {
        AddLog("Read item: " + item);
    }
}

public class MyType {
    public string id { get; set; }
    public DateTime date { get; set; }
    public int myfield { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM