I want to create U.S. Date Format to Indian Date Format Using Asp MVC Core 2.0 - asp.net-core

I am trying to Create Date Format the US to Indian Date Format like(dd/mm/yyyy hh:mm tt).
When I run the code on my local machine it works.
When we publish and fetch values from the server at that time it shows "US" Date Format(mm/dd/yyyy)
How τo do the internal conversion, in Appsettings.json what strings i need to mention.
public static DateTime ConvertIndianDateFormat(DateTime usTime)
{
DateTime dateTime = DateTime.Now;
TimeZoneInfo usEasternZone = TimeZoneInfo.FindSystemTimeZoneById("US Eastern Standard Time");
TimeZoneInfo indianZone = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
DateTime usEasternTime = TimeZoneInfo.ConvertTimeFromUtc(usTime, usEasternZone);
DateTime indianTime = TimeZoneInfo.ConvertTimeFromUtc(usTime, indianZone);
return indianTime;
}

This is because you are probably using something like DateTime.Now for C# and if you have an SQL Server you are using GETDATE(). It's not like an issue with application.json or something. The above functions return the machine datetime, thus why locally on your pc the time is correct and incorrect if you upload it to a server.
So make sure that the time is correct. If you are uploading to servers in another country then you will probably have a different time and/or format.
How you proceed depends on your needs:
Is the time correct?
Then simply reformated it or store it specifically using
DateTime.Now.ToString("dd/mm/yyyy hh:mm tt") // this is not the correct format.
Do you want to serve multiple clients in multiple regions/countries?
Then you should store the time as UTC and cast it based on clients date format. For example the server could be in USA and someone from UK would view a different time than his own which would be weird.
DateTime.UtcNow
Generally your problem could be large or small depending on your needs

Related

Pentaho kettle convert date to unix

I'm trying to pacha a string format dated "2019-05-14 13:30:00" to a UNIX format.
In javascript I got it but in the javascript kettle module I am not able to return the numeric value 1557833442
the line of code is this:
const tests = (new Date ("2019-05-14 13:30:00"). getTime () / 1000);
It looks like the Date() constructor doesn't like the format you are using.
If you want the current date, use a Get System Info, it has a number of useful date options.
If you are converting an incoming field, use the Select Values step to change the metadata, using the format string that matches your string field's format.

Send UTC timestamps with moment.js to ASP.NET Core controller

I have a database with measured values from devices that I want to display in a web frontend. First I send the list of devices to the frontend together with the IANA timezone specifier for each device.
I would like all timestamps to be exchanged as UTC. The user selects a time range in the frontend in device-local time. I use moment.js to convert these timestamps to UTC with the known timezone of the device like this:
var startTimestamp = new Date(2017, 7, 1); //some local timestamp (zero-based month!)
var m = moment.tz(startTimestamp, "Europe/Berlin");
var utc = moment.utc(m).format();
utc is now "2017-07-31T22:00:00Z" which seems to be correct given the 2 hours offset for Berlin in DST.
I send this utc timestamp to my ASP.NET Core backend. The controller looks like this:
[HttpGet]
public IEnumerable<TimestampedValue> GetValues(int id, DateTime startTimestamp)
{
...
}
The problem is that startTimestamp is 2017-08-01 00:00:00 when the controller is called and its Kind property is set to Local. I would have expected it to be the same UTC timestamp.
Any idea what I'm doing wrong? I think moment.js is doing its job correctly so this must be a problem on the server side. If I recall correctly, the deserialization is done by JSON.net but I don't understand why it does not respect the Z at the end of the time string.
After #dbc pointed me to the different behavior between GET and POST requests I come to this conclusion:
Since my request uses the GET method and query strings are not JSON, there is no JSON.net involved in the problem, it is the default .NET Core DateTimeConverter that does the conversion. Moment.js correctly converts the timestamp to a UTC string, I checked that using the browser developer tools.
The code for DateTimeConverter can be found here:
https://github.com/dotnet/corefx/blob/312736914d4e98c2948778bacac029aa831dd6b5/src/System.ComponentModel.TypeConverter/src/System/ComponentModel/DateTimeConverter.cs
As can be seen there, the converter uses DateTime.Parse. It can be tested in a simple test project that DateTime.Parse does not respect the Z-suffix. This is also discussed here DateTimeConverter converting from UTC string.
I think there would be at least four solutions
1) write a custom model binder. These SOs each show a part of it Custom DateTime model binder in ASP.NET Core 1 (RTM)
https://dotnetcoretutorials.com/2016/12/28/custom-model-binders-asp-net-core/
2) write a custom type converter that overrides the default DateTime converter and checks whether there is a trailing Z. If so, use DateTime.Parse with the DateTimeStyles.AdjustToUniversal. Else fall back to the default implementation. I like this solution but I currently don't know how to replace the default DateTimeConverter.
3) replace all relevant DateTime parameters in the controllers with DateTimeOffset. DateTimeOffset seems to correctly convert the UTC string.
4) use a POST instead of a GET request with JSON in the request body. JSON.net seems to correctly convert the UTC string.
My preferred solution is currently a mixture of 3 and 4, depending on the context.

Swift 3 playground logs dates in local format. How?

If you run a line like this in a Playground in the US:
let today = Date()
You'll see output like this to the right of the source code:
"Sep 26, 2016, 8:17 PM"
That appears to be the date, displayed in the local time zone, using medium date and time style.
How does that work?
If you try to print the date:
print("today = \(today)"
You'll see "Today = 2016-09-27 00:18:55 +0000\n", which is UTC, and appears to be unix date format.
What function is the Playground using to display the date when you first create a date? Is there a way to get to that output format from code or from the debug console?
Up until now I've created a date formatter that I use to log dates, display them in the console, etc.
It's lurking in CustomPlaygroundQuickLookable protocol, which Date conforms to:
if case .text(let str) = today.customPlaygroundQuickLook {
print(str)
}

Compare dates in openerp 7

I have a custom module in openerp 7 with fields check-in time(date-time) and check-out time(date-time). When I click on save, i want to perform a validation on both fields to ensure check-out time is not less than check-in time. Thanks for any ideas.
As above, use datetime.
In Odoo your dates, times and datetimes are handed to use as strings formatted using
openerp.tools.DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_TIME_FORMAT and DEFAULT_SERVER_DATETIME_FORMAT.
from datetime import datetime
from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT
check_in = datetime.strptime(my_object.check_in, DEFAULT_SERVER_DATETIME_FORMAT)
check_out = datetime.strptime(my_object.check_out, DEFAULT_SERVER_DATETIME_FORMAT)
Go nuts with comparisons etc.
A couple of notes:
I highly recommend reading up on the datetime module in the standard library, particularly strftime, strptime and timedelta
Remember you will be getting the dates and datetimes in UTC. The classes that represent the date and datetime fields have methods to return dates and timestamps in the users' timezone but you will not usually need these. Have a look at fields.date.context_today and fields.datetime.context_timestamp
I would try to use the datetime class from the datetime module.
Import relevant python module
from datetime import datetime
Retrieve your record via the appropriate method i.e.
your_record = self.pool.get('your_custom_module').search(cr, uid, domain, offset=0, limit=None, order=None, context=None, count=False)
note: you need to provide proper domain and modify/remove arguments to suit you needs
Create datetime objects from relevant fields (use the strptime method of datetime class : create a date object from a string). Something like :
check_in = datetime.strptime(your_record[0]['check-in time'], '%Y-%m-%d')
check_out = datetime.strptime(your_record[0]['check-out time'], '%Y-%m-%d')
note: you need to adapt the format('%Y-%m-%d') to whatever format your DB returns
Compare both object with a simple expression :
if check_in < check_out:
...
else:
...
Do whatever other operations need to be done.
It's kinda hard to provide more info without additional details about your flow.
Hope this helps,
Cheers

SQL Server 2008 SSIS: Importing date field incorrectly (from Oracle)

The date fields in the file (CSV) I must import are in the format DD-MMM-YY, SQL Server is importing them as YY-MM-DD. So 01-FEB-13 is imported as 2001-02-13.
How can I change the format SQL uses to import the dates?
I don't have access to the original Oracle database, only the flat file exported from it. So everything I do pretty much has to be done in SQL.
Changing the date format that SQL Server uses by default would require mucking around with the Windows server culture settings. Not a good idea, especially if this is the only file where you're having this issue.
I would use a Script Transformation and the .NET Framework's DateTime.ParseExact method, which lets you completely control the expected format. Start by configuring the Flat File Connection Manager that you're using to read the CSV files so that the columns with the DD-MMM-YY dates have their DataType set to string or Unicode string rather than date, database date or any other date-specific type:
In your Data Flow, place a Script Transformation between your source and destination components, like thus:
Select each of the DD-MMM-YY date columns as inputs to the Script Transformation:
And create an output column with a DataType of date corresponding to each input column:
The Script Transformation code will look like this (using C#)
using System;
using System.Data;
using System.Globalization; // needed for CultureInfo.InvariantCulture
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
public override void IncomingRows_ProcessInputRow(IncomingRowsBuffer Row)
{
string incomingFormat = "dd-MMM-yy";
Row.ConvertedCreateDate = DateTime.ParseExact(
Row.CreateDate,
incomingFormat,
CultureInfo.InvariantCulture);
Row.ConvertedLastUpdateDate = DateTime.ParseExact(
Row.LastUpdateDate,
incomingFormat,
CultureInfo.InvariantCulture);
}
}
You can use the to_char() function in oracle to format your dates so that they are in the format you want. Hopefully Gordon's comment about storing as text referred to a staging table. For a variety of reasons, your first import should be into a staging table anyway.