0

I am new to Angular and trying to pass date from the angular component to MVC API(C#) date time. Looks like I am not getting default time stamp 12:00 am in API method. Let me know what I am doing wrong.

Client Model:

export class TestClass {
    constructor(
        public ctyName: string = '',
        public stateName: string = '',
        public zipCode: string = '',
        public country: string = '',
        public effectiveDate: Date = null

    ) {
    }
}

Service Model

public class TestClass 
{          
    public string CityName { get; set; }   
    public string StateName { get; set; }    
    public string ZipCode { get; set; }    
    public string Country { get; set; }    
    public DateTime EffectiveDate: { get; set; }        
}

Client Method - Assignment

var month = '1';
var day = '1';
var year = '2016';
effectiveDate = new Date(year, month - 1, day);

Api Method

[HttpPost("[action]/{id}")]
public PostResult UpdateData(string id, [FromBody]TestClass testClass)
{
}

Value I am receiving in API

2016-01-01 05:00:00.000

Thanks for your time

Muhammed Shevil KP
  • 1,404
  • 1
  • 16
  • 21
Vinod
  • 343
  • 1
  • 4
  • 14
  • Are you sure that display of the DateTime is not adjusting for the application timezone. – PaulF Dec 05 '17 at 16:00
  • When debugging the JS and the .NET: Are both time variables in the same timezone? – OlafW Dec 05 '17 at 16:14
  • How do I find out JS variable timezone. Its a date variable in JS. – Vinod Dec 05 '17 at 16:23
  • I added Date.UTC and I am getting 12:00 am. In Component - new Date(Date.UTC(year, month - 1, day)); Do I have to do that for every date field in all components or is there any global settings for date in the client angular application to use UTC? – Vinod Dec 05 '17 at 16:29

1 Answers1

0

Please try to change this line:

effectiveDate = new Date(year, month - 1, day);

to this:

effectiveDate = new Date(year, month - 1, day, 0, 0, 0, 0);

This should help you.

The case is the signature of JS Date constructor is:

new Date(year, month, day, hours, minutes, seconds, milliseconds);

In your case you only specifying year, month and day. But hours, minutes and seconds are filled automatically by your current local time.

Check this for more reference:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Taras Ilyin
  • 101
  • 5
  • @Vinod - When creating a date in JavaScript, it keeps your time zone information. If that info is not stripped before posting to the server, the server will see it and keep it around. You will have to determine if losing the time zone offset information will affect your application or not. If it doesn't matter, you can do what this answer suggests, but on the server (not the client). If it does matter, you should accept the time zone offset info and manipulate it or store it as necessary. – ps2goat Dec 05 '17 at 16:49
  • new Date(year, month - 1, day,0,0,0,0); didn't work. I am still getting 4 am in the time stamp. Adding new Date(Date.UTC(year, month - 1, day)); helped me to get 12:00 am.Any thoughts? – Vinod Dec 05 '17 at 16:50
  • @Vinod - I updated my comment. You would need to create a new date on the server based on month, day, and year passed in. My preferred way of handling this is to create a date string on the client, and pass that in to the server. I would also make the property on the server object a string, and parse it to make sure it is a valid date, but you may not need that step. – ps2goat Dec 05 '17 at 16:52
  • Seems very unlikely that the bit of code ran at precisely 5am down to the milliscond – PaulF Dec 05 '17 at 16:52
  • @PaulF - `new Date` would default to midnight. A time zone offset of +5 GMT would make it look like 5 am to a server running in UTC. For example, if I run `new Date("1/2/2017")` in my browser console, the output includes my offset: `Mon Jan 02 2017 00:00:00 GMT-0600 (Central Standard Time)` When sending that to a server running in UTC, it converts to UTC. In my case, it would be treated as "1/2/2017 6:00" (am) – ps2goat Dec 05 '17 at 16:56
  • @ps2goat: Hence my original comment to the question. I was responding to this statement in this answer _"But hours, minutes and seconds are filled automatically by your current local time."_ – PaulF Dec 05 '17 at 16:58
  • @ps2goat Thanks for your reply and suggestion. I don't prefer sending a string to server side. Will converting to UTC solve this issue new Date(Date.UTC(year, month - 1, day));? Our application does not care about time zone offset. We always require 12:00 am. Thanks for your time. – Vinod Dec 05 '17 at 17:00
  • @Vinod - It looks like that would work (for the server). But on the client, it would look like the day prior. E.g., on mine: `new Date(Date.UTC(2017, 1, 2))` produces `Wed Feb 01 2017 18:00:00 GMT-0600 (Central Standard Time)`. (18 + 6 = 1/2/2017 in UTC) If you use the value locally in the browser code, this may cause issues depending on how your app works. I use moment.js to create the date and use the `.format('YYYY-MM-DD')` method to send a string to the server that is in the ISO date format. – ps2goat Dec 05 '17 at 17:32
  • Thanks. For now as a workaround I am taking effectiveDate.Date on server side and saving it to the database (which works). As I mentioned I am not worried about the time stamp – Vinod Dec 05 '17 at 19:12