DateTime value changes after I post it on server and then receive it back












1















I use DateTime.Now method to get local time on machine and then post it with model to ASP.NET server.



The DateTime value I post is:



The DateTime value i post



The DateTime value I get from server is:



The DateTime value i get from server



So as you can see in the screenshots two values are different. My problem is that they should be the same.



Post method on ASP.Net server:



public async Task<IHttpActionResult> PostTimeCap(TimeCap timeCap)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

List<TimeCap> l = db.TimeCaps.ToList();
TimeCap lastCap;

if (l.Count > 0)
{
lastCap = l[l.Count - 1];

for (int i = l.Count - 1; i >= 0; i--)
{
if (l[i].userind == timeCap.userind && lastCap.date < l[i].date && !l[i].isPairDefined)
{
lastCap = l[i];
}
}
}
else
{
lastCap = null;
}

if (timeCap.typeOfCap == "open")
{
}
else if(timeCap.typeOfCap == "overwork")
{
}
else if(timeCap.typeOfCap == "close")
{
if (lastCap != null)
{
if (lastCap.typeOfCap == "open" || lastCap.typeOfCap == "overwork")
{
timeCap.isPairDefined = true;
timeCap.pairIndex = lastCap.id;
lastCap.isPairDefined = true;
await PutTimeCap(lastCap.id, lastCap);
double totalSec = (timeCap.date - lastCap.date).TotalSeconds;
totalSec = totalSec / 3600;
timeCap.workhours = (float)totalSec;
}
else
{
//DeleteTimeCap(lastCap.id);
//TimeCap lc = FindLastCap(timeCap.userind);
//timeCap.isPairDefined = true;
//timeCap.pairIndex = lc.id;
}
}
}

db.TimeCaps.Add(timeCap);
await db.SaveChangesAsync();

return CreatedAtRoute("DefaultApi", new { id = timeCap.id }, timeCap);
}


Get method on the server:



public IQueryable<TimeCap> GetTimeCaps()
{
return db.TimeCaps;
}


On the client:



TimeCap t = new TimeCap
{
date = DateTime.Now,
typeOfCap = "overwork",
userind = DataStaticStorage.userId,
isPairDefined = false,
isVerifyed = false,
pairIndex = 0,
imgIndex = imgstr,
workhours = 0f
};


And when I get from server:



t.dateStr = tp[i].date.ToString("F");


I need to post a datetime and then receive it back without changes.



Do I miss a big shard of knowledge about DateTime and Asp.net?



My local time is GMT +6 (Kazakhstan, Astana)










share|improve this question




















  • 1





    you are sending a Local DateTime, and the server is returning an Unspecified DateTime. To eliminate confusion it's generally best to create the client DateTime as UTC, not Local.

    – Jason
    Nov 25 '18 at 16:07













  • Ok now I get it. I post DateTime like this: DateTime now = DateTime.UtcNow and I get it like this: response.date.ToLocalTime() and its working like I want it to. Thank you Jason, this site is real help ; )

    – Дархан Сыдыков
    Nov 25 '18 at 17:39











  • Hello, @ДарханСыдыков! I had the same issue. It is because of different formats of datetime. You have to set format for your datetime without milliseconds.

    – Денис Чорный
    Nov 26 '18 at 4:13


















1















I use DateTime.Now method to get local time on machine and then post it with model to ASP.NET server.



The DateTime value I post is:



The DateTime value i post



The DateTime value I get from server is:



The DateTime value i get from server



So as you can see in the screenshots two values are different. My problem is that they should be the same.



Post method on ASP.Net server:



public async Task<IHttpActionResult> PostTimeCap(TimeCap timeCap)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

List<TimeCap> l = db.TimeCaps.ToList();
TimeCap lastCap;

if (l.Count > 0)
{
lastCap = l[l.Count - 1];

for (int i = l.Count - 1; i >= 0; i--)
{
if (l[i].userind == timeCap.userind && lastCap.date < l[i].date && !l[i].isPairDefined)
{
lastCap = l[i];
}
}
}
else
{
lastCap = null;
}

if (timeCap.typeOfCap == "open")
{
}
else if(timeCap.typeOfCap == "overwork")
{
}
else if(timeCap.typeOfCap == "close")
{
if (lastCap != null)
{
if (lastCap.typeOfCap == "open" || lastCap.typeOfCap == "overwork")
{
timeCap.isPairDefined = true;
timeCap.pairIndex = lastCap.id;
lastCap.isPairDefined = true;
await PutTimeCap(lastCap.id, lastCap);
double totalSec = (timeCap.date - lastCap.date).TotalSeconds;
totalSec = totalSec / 3600;
timeCap.workhours = (float)totalSec;
}
else
{
//DeleteTimeCap(lastCap.id);
//TimeCap lc = FindLastCap(timeCap.userind);
//timeCap.isPairDefined = true;
//timeCap.pairIndex = lc.id;
}
}
}

db.TimeCaps.Add(timeCap);
await db.SaveChangesAsync();

return CreatedAtRoute("DefaultApi", new { id = timeCap.id }, timeCap);
}


Get method on the server:



public IQueryable<TimeCap> GetTimeCaps()
{
return db.TimeCaps;
}


On the client:



TimeCap t = new TimeCap
{
date = DateTime.Now,
typeOfCap = "overwork",
userind = DataStaticStorage.userId,
isPairDefined = false,
isVerifyed = false,
pairIndex = 0,
imgIndex = imgstr,
workhours = 0f
};


And when I get from server:



t.dateStr = tp[i].date.ToString("F");


I need to post a datetime and then receive it back without changes.



Do I miss a big shard of knowledge about DateTime and Asp.net?



My local time is GMT +6 (Kazakhstan, Astana)










share|improve this question




















  • 1





    you are sending a Local DateTime, and the server is returning an Unspecified DateTime. To eliminate confusion it's generally best to create the client DateTime as UTC, not Local.

    – Jason
    Nov 25 '18 at 16:07













  • Ok now I get it. I post DateTime like this: DateTime now = DateTime.UtcNow and I get it like this: response.date.ToLocalTime() and its working like I want it to. Thank you Jason, this site is real help ; )

    – Дархан Сыдыков
    Nov 25 '18 at 17:39











  • Hello, @ДарханСыдыков! I had the same issue. It is because of different formats of datetime. You have to set format for your datetime without milliseconds.

    – Денис Чорный
    Nov 26 '18 at 4:13
















1












1








1








I use DateTime.Now method to get local time on machine and then post it with model to ASP.NET server.



The DateTime value I post is:



The DateTime value i post



The DateTime value I get from server is:



The DateTime value i get from server



So as you can see in the screenshots two values are different. My problem is that they should be the same.



Post method on ASP.Net server:



public async Task<IHttpActionResult> PostTimeCap(TimeCap timeCap)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

List<TimeCap> l = db.TimeCaps.ToList();
TimeCap lastCap;

if (l.Count > 0)
{
lastCap = l[l.Count - 1];

for (int i = l.Count - 1; i >= 0; i--)
{
if (l[i].userind == timeCap.userind && lastCap.date < l[i].date && !l[i].isPairDefined)
{
lastCap = l[i];
}
}
}
else
{
lastCap = null;
}

if (timeCap.typeOfCap == "open")
{
}
else if(timeCap.typeOfCap == "overwork")
{
}
else if(timeCap.typeOfCap == "close")
{
if (lastCap != null)
{
if (lastCap.typeOfCap == "open" || lastCap.typeOfCap == "overwork")
{
timeCap.isPairDefined = true;
timeCap.pairIndex = lastCap.id;
lastCap.isPairDefined = true;
await PutTimeCap(lastCap.id, lastCap);
double totalSec = (timeCap.date - lastCap.date).TotalSeconds;
totalSec = totalSec / 3600;
timeCap.workhours = (float)totalSec;
}
else
{
//DeleteTimeCap(lastCap.id);
//TimeCap lc = FindLastCap(timeCap.userind);
//timeCap.isPairDefined = true;
//timeCap.pairIndex = lc.id;
}
}
}

db.TimeCaps.Add(timeCap);
await db.SaveChangesAsync();

return CreatedAtRoute("DefaultApi", new { id = timeCap.id }, timeCap);
}


Get method on the server:



public IQueryable<TimeCap> GetTimeCaps()
{
return db.TimeCaps;
}


On the client:



TimeCap t = new TimeCap
{
date = DateTime.Now,
typeOfCap = "overwork",
userind = DataStaticStorage.userId,
isPairDefined = false,
isVerifyed = false,
pairIndex = 0,
imgIndex = imgstr,
workhours = 0f
};


And when I get from server:



t.dateStr = tp[i].date.ToString("F");


I need to post a datetime and then receive it back without changes.



Do I miss a big shard of knowledge about DateTime and Asp.net?



My local time is GMT +6 (Kazakhstan, Astana)










share|improve this question
















I use DateTime.Now method to get local time on machine and then post it with model to ASP.NET server.



The DateTime value I post is:



The DateTime value i post



The DateTime value I get from server is:



The DateTime value i get from server



So as you can see in the screenshots two values are different. My problem is that they should be the same.



Post method on ASP.Net server:



public async Task<IHttpActionResult> PostTimeCap(TimeCap timeCap)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

List<TimeCap> l = db.TimeCaps.ToList();
TimeCap lastCap;

if (l.Count > 0)
{
lastCap = l[l.Count - 1];

for (int i = l.Count - 1; i >= 0; i--)
{
if (l[i].userind == timeCap.userind && lastCap.date < l[i].date && !l[i].isPairDefined)
{
lastCap = l[i];
}
}
}
else
{
lastCap = null;
}

if (timeCap.typeOfCap == "open")
{
}
else if(timeCap.typeOfCap == "overwork")
{
}
else if(timeCap.typeOfCap == "close")
{
if (lastCap != null)
{
if (lastCap.typeOfCap == "open" || lastCap.typeOfCap == "overwork")
{
timeCap.isPairDefined = true;
timeCap.pairIndex = lastCap.id;
lastCap.isPairDefined = true;
await PutTimeCap(lastCap.id, lastCap);
double totalSec = (timeCap.date - lastCap.date).TotalSeconds;
totalSec = totalSec / 3600;
timeCap.workhours = (float)totalSec;
}
else
{
//DeleteTimeCap(lastCap.id);
//TimeCap lc = FindLastCap(timeCap.userind);
//timeCap.isPairDefined = true;
//timeCap.pairIndex = lc.id;
}
}
}

db.TimeCaps.Add(timeCap);
await db.SaveChangesAsync();

return CreatedAtRoute("DefaultApi", new { id = timeCap.id }, timeCap);
}


Get method on the server:



public IQueryable<TimeCap> GetTimeCaps()
{
return db.TimeCaps;
}


On the client:



TimeCap t = new TimeCap
{
date = DateTime.Now,
typeOfCap = "overwork",
userind = DataStaticStorage.userId,
isPairDefined = false,
isVerifyed = false,
pairIndex = 0,
imgIndex = imgstr,
workhours = 0f
};


And when I get from server:



t.dateStr = tp[i].date.ToString("F");


I need to post a datetime and then receive it back without changes.



Do I miss a big shard of knowledge about DateTime and Asp.net?



My local time is GMT +6 (Kazakhstan, Astana)







asp.net date datetime time xamarin.forms






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 15:54









marc_s

581k13011221268




581k13011221268










asked Nov 25 '18 at 14:39









Дархан СыдыковДархан Сыдыков

134




134








  • 1





    you are sending a Local DateTime, and the server is returning an Unspecified DateTime. To eliminate confusion it's generally best to create the client DateTime as UTC, not Local.

    – Jason
    Nov 25 '18 at 16:07













  • Ok now I get it. I post DateTime like this: DateTime now = DateTime.UtcNow and I get it like this: response.date.ToLocalTime() and its working like I want it to. Thank you Jason, this site is real help ; )

    – Дархан Сыдыков
    Nov 25 '18 at 17:39











  • Hello, @ДарханСыдыков! I had the same issue. It is because of different formats of datetime. You have to set format for your datetime without milliseconds.

    – Денис Чорный
    Nov 26 '18 at 4:13
















  • 1





    you are sending a Local DateTime, and the server is returning an Unspecified DateTime. To eliminate confusion it's generally best to create the client DateTime as UTC, not Local.

    – Jason
    Nov 25 '18 at 16:07













  • Ok now I get it. I post DateTime like this: DateTime now = DateTime.UtcNow and I get it like this: response.date.ToLocalTime() and its working like I want it to. Thank you Jason, this site is real help ; )

    – Дархан Сыдыков
    Nov 25 '18 at 17:39











  • Hello, @ДарханСыдыков! I had the same issue. It is because of different formats of datetime. You have to set format for your datetime without milliseconds.

    – Денис Чорный
    Nov 26 '18 at 4:13










1




1





you are sending a Local DateTime, and the server is returning an Unspecified DateTime. To eliminate confusion it's generally best to create the client DateTime as UTC, not Local.

– Jason
Nov 25 '18 at 16:07







you are sending a Local DateTime, and the server is returning an Unspecified DateTime. To eliminate confusion it's generally best to create the client DateTime as UTC, not Local.

– Jason
Nov 25 '18 at 16:07















Ok now I get it. I post DateTime like this: DateTime now = DateTime.UtcNow and I get it like this: response.date.ToLocalTime() and its working like I want it to. Thank you Jason, this site is real help ; )

– Дархан Сыдыков
Nov 25 '18 at 17:39





Ok now I get it. I post DateTime like this: DateTime now = DateTime.UtcNow and I get it like this: response.date.ToLocalTime() and its working like I want it to. Thank you Jason, this site is real help ; )

– Дархан Сыдыков
Nov 25 '18 at 17:39













Hello, @ДарханСыдыков! I had the same issue. It is because of different formats of datetime. You have to set format for your datetime without milliseconds.

– Денис Чорный
Nov 26 '18 at 4:13







Hello, @ДарханСыдыков! I had the same issue. It is because of different formats of datetime. You have to set format for your datetime without milliseconds.

– Денис Чорный
Nov 26 '18 at 4:13














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53468594%2fdatetime-value-changes-after-i-post-it-on-server-and-then-receive-it-back%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53468594%2fdatetime-value-changes-after-i-post-it-on-server-and-then-receive-it-back%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen