【问题标题】:How to convert "string" to "timestamp without time zone"如何将“字符串”转换为“没有时区的时间戳”
【发布时间】:2013-09-25 15:11:51
【问题描述】:

我是 Postgresql 新手,我正在使用 WCF 服务。
这是我的代码 sn-p:

$.ajax({
    url: '../Services/AuctionEntryServices.svc/InsertAuctionDetails',
    data: JSON.stringify({ "objAuctionEntryEntity": {
        "AuctionNO": '',          
        "AuctionDate": $('[Id$="lblAuctionDateVal"]').text(),
        "TraderID": $('[Id$="ddlTraderName"] option:selected').val(),
        "Grade": $('[Id$="ddlGrade"] option:selected').val(),
        "Varity": $('[Id$="ddlVarity"] option:selected').val(), 
        "QuntityInAuction": $('#txtQuantityForAuction').val(),
        "AuctionRate": $('#txtAuctionRate').val(),
        "BrokerID": a[0],
        "IsSold": $('#chlIsSold').is(':checked'),
        "CreatedBy": $.parseJSON(GetCookie('Admin_User_In_Mandi')).UserID,
        "UpdatedBy": $.parseJSON(GetCookie('Admin_User_In_Mandi')).UserID,
        "CreationDate": GetCurrentDate().toMSJSON(),
        "IsActive": true,
        "AuctionTransaction": arrAuctionTransaction,
        "MandiID": $.parseJSON(GetCookie('Admin_User_In_Mandi')).MandiID,
        "FarmerID": _ownerid,
        "AuctionNO": _auctionno,
        "AmmanatPattiID": _ammantpattiid,
        "ToTraderID": b[0],
        "ToTraderName": $('#txtOtherBuyerNameEN').val(),
        "ToTraderName_HI": $('#txtOtherBuyerNameHI').val()
    }
}),
    type: 'POST',
    contentType: 'application/json',
    dataType: 'json'              
});

这里:

$('[Id$="lblAuctionDateVal"]').text() = "20/8/2013 14:52:49" 

我的这个字段的数据类型是timestamp without time zone
如何将此字符串转换为timestamp without time zone数据类型?

【问题讨论】:

标签: javascript jquery ajax postgresql wcf


【解决方案1】:

timestamp (= timestamp without time zone) 的字符串表示取决于您的区域设置。因此,为避免歧义导致数据错误或 Postgres 产生异常,您有两种选择:

1.) 使用ISO 8601 format,与任何 语言环境或DateStyle 设置相同:

'2013-08-20 14:52:49'

在数据类型无法从上下文派生的情况下,您可能必须显式转换字符串文字,具体取决于用例:

'2013-08-20 14:52:49'::timestamp

2.) 使用带有匹配模板模式的to_timestamp() 将字符串转换为timestamp

to_timestamp('20/8/2013 14:52:49', 'DD/MM/YYYY hh24:mi:ss')

这将返回timestamptz,假设当前时区设置。通常(如在分配中)类型会被相应地强制。对于timestamp,这意味着时间偏移被截断并且您得到期望值。 同样,如果目标类型不能从上下文派生,您可能必须显式转换:

to_timestamp('20/8/2013 14:52:49', 'DD/MM/YYYY hh24:mi:ss')::timestamp

由于这只是去除时间偏移,因此会产生预期值。或者使用 AT TIME ZONE 结构和您选择的时区:

to_timestamp('20/8/2013 14:52:49', 'DD/MM/YYYY hh24:mi:ss') AT TIME ZONE 'UTC'

虽然目标时区与您当前的timezone 设置相同,但不会发生任何转换。否则,生成的时间戳会相应地转置。延伸阅读:

【讨论】:

    【解决方案2】:

    要将字符串转换为没有时区的时间戳,对于Postgresql,我使用上面的

    SELECT to_timestamp('23-11-1986 09:30:00', 'DD-MM-YYYY hh24:mi:ss')::timestamp without time zone;
    

    【讨论】:

    • 你好,非常感谢朋友。我刚刚失去了最后 5 个小时来搜索这个.....
    猜你喜欢
    • 2021-08-30
    • 1970-01-01
    • 2011-03-19
    • 2020-11-22
    • 1970-01-01
    • 2017-04-25
    • 2018-08-27
    • 2015-06-11
    • 1970-01-01
    相关资源
    最近更新 更多