从db RESTful Web API最佳实践传递日期

时间:2018-09-24 09:03:21

标签: c# sql-server date asp.net-web-api

所以我的数据库中有两种日期类型,date (yyyy-mm-dd)datetimeoffset (yyyy-mm-ddThh:mm:ss.ms+Z),我想知道从数据库中获取数据时最好的做法是什么。将其作为json传递到UI /移动设备。

我以前总是将日期解析为datetimeoffset,因此正常日期将类似于2018-09-24T00:00:00.000+00:00,而不是简单的2018-09-24,但它与已经保存的datetimeoffset完美地结合在一起在数据库中

2 个答案:

答案 0 :(得分:1)

您可以通过多种方式处理这种情况。

1:从API端开始,始终提供预定义的日期格式值

示例yyyy-mm-ddThh:mm:ss.ms+Z

然后根据条件从客户端进行转换。

2:保留不同的View模型/属性可能是用于存储yyyy-mm-dd的,您可以给string数据类型,而对于yyyy-mm-ddThh:mm:ss.ms+Z的只是DateTime。根据您的数据库,您可以编写条件并映射特定数据。

3:保留一个用于返回日期的属性,并将其设置为string

示例:Public string CurrentDate{get;set;},您可以简单地映射数据库值(应完成转换)。在这种情况下,客户无需担心日期转换,他们可以简单地显示您从api传递的信息。

注意:方法3并非可取,因为在某些情况下,用户可能在其他地方yyyy-mm-dd看到yyyy-mm-ddThh:mm:ss.ms+Z

答案 1 :(得分:0)

看看SQL Server Data Type Mappings
您将看到SQL Server的import { Component, OnInit, Input } from '@angular/core'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Router, ActivatedRoute, ParamMap } from '@angular/router'; import { User } from './users'; @Component({ selector: 'app-users', templateUrl: './users.component.html', styleUrls: ['./users.component.css'] }) export class CreateUsersComponent implements OnInit { user: User = new User(); constructor(private http: HttpClient, private router: Router) { console.log("constructor call"); } ngOnInit() { console.log("Init function called"); } public onEdit(userObj){ this.router.navigate(['/user']) this.user =userObj; console.log("Value set in user property of component") } save(){ let url = "/users"; console.log(this.user); this.http.post(url, this.user) .subscribe((response)=> { console.log("saved"); }); } } import { User } from '../users/user'; import { HttpClient } from '@angular/common/http'; import { CreateUsersComponent } from "../users/createUser.component"; import { Component, OnInit, ViewChild } from '@angular/core'; import { Router, ActivatedRoute, ParamMap } from '@angular/router'; import { MatPaginator, MatSort, MatTableDataSource } from '@angular/material'; @Component({ providers:[ CreateUsersComponent ], selector: 'manage-users', templateUrl: './manage-users.component.html', styleUrls: ['./manage-users.component.css'] }) export class ManageUsersComponent implements OnInit { @ViewChild(MatPaginator) paginator: MatPaginator; @ViewChild(MatSort) sort: MatSort; items: any; dataSource = new MatTableDataSource<User>(); displayedColumns: string[] = ['id', 'name', 'age', 'address', 'action']; applyFilter(filterValue: string) { this.dataSource.filter = filterValue.trim().toLowerCase(); if (this.dataSource.paginator) { this.dataSource.paginator.firstPage(); } } getAllUsers(){ let url = "/users" this.http.get(url) .subscribe((response)=> { console.log(response); this.items = response; this.dataSource.data = this.items; }); } edit(userObj){ // Calling onEdit function of CreateUsersComponent passing the userObj this.userComponent.onEdit(userObj); } constructor(private userComponent: CreateUsersComponent, private http: HttpClient) { } ngOnInit() { this.dataSource.paginator = this.paginator; this.dataSource.sort = this.sort; this.getAllUsers(); } } Date都映射到.Net的DateTime数据类型,
DateTime2映射到DateTime

DateTimeOffset