So I'm pulling a DateTime from a database and I want to display it in an HTML text input (<input type="text">).
By the time the C# DateTime is sent to Javascript, it looks something like this: fooDate: {6/22/2011 2:30:00 PM}. But when it is displayed in the <input>, it ends up looking like this: 2011-06-22T14:30:00.
It's kind of annoying because I only care about displaying the date. Why is this this conversion happening, and is there a way to just get it to display the 6/22/2011 2:30:00 PM bit? Can this be accomplished without the use of regular expressions?
Mix and match whatever combination you want to. World is your oyster.
DateTime.Now.Day
DateTime.Now.Month
DateTime.Now.Year
are you able to convert it to string before you pass it to your JS-Method?
string Output = string.Format("{0:G}",DatabaseValue);
Examples for just getting at the date...
DateTime.Now.ToShortDateString();
will return "02/12/2014"
Or you can specify the format you want
DateTime.Now.ToString("yyyy-MM-dd");
will return "2014-12-02"
Try using the String.Format function:
//Here you pull the DateTime from your db
//I show you an example with the current DateTime
DateTime myDateTime = DateTime.Now;
string date = String.Format("{0:dd-MM-yyyy}", myDateTime); //eg: 02-12-2014
//Place the string in your textbox
this.txtYourTextbox.Text = date;
if you don't know which format to use, refer to following article:
String Format for DateTime [C#]
Related
2019-11-19 15:35:52.494
This is actual data in my database table, but in the datatable I get this:
2019-11-19T10:05
The date format is wrong.
How can i solve this?
The date format is wrong.
The format is not wrong, The T is just a standard (ISO 8601) way to de-limit the time.
To remove T from your datetime string you could format your string using pure Javascript something like this or you can use any other library like moment. Take a look here for formatting using moment js.
In columnDefs, add your target column
$(document).ready( function () {
var table = $('#example').DataTable({
columnDefs:[{targets:4, render:function(data){
return moment(data).format('MMMM Do YYYY');
}}]
});
});
In targets put your column number and In format put the format you want to display. You can try with different format follow this below link
click here for demo
If you don't necessarily need it to be a date type, you could try to cast the timestamp to text in your select statement when you retrieve it from the database, like this:
SELECT TO_CHAR(NOW(),'dd/MM/YYYY hh:mm:ss') AS date_time
Here is more info about formatting using TO_CHAR.
Our previous developer create a generic method that retrieve all user input that has an update/changes.
it looks like this (user side/JavaScript/Kendo):
param._updated = JSON.stringify(rows._updated);
I am somehow desperate that when that *rows._updated contains a Date Value uncle JSON convert it into other String format that result to DateTime Difference for example:
dateField = 11/1/2015 // <--Original User Input
rows._updated = { dateField: November 1, 2015 0:00:00 GMT+080 ... }
param._updated = { "dateField": "2015-10-31T16:00:00.0000Z"... }
which would now result to a conflict.
since the above code was generic field that might contain different data and type, I am trying to solve this issues at the server side but i failed to achieve the original date value.
NOTE: our users has 2-5 different timezone so it's kinda hard to hard code the conversion.
related issues: here
It's getting late. Thanks in advance!.
I somehow achieve what I want to by the following approach
1.) At backend Convert the DateTime to UTC Format
string dateUTC = paramDate.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'");
2.) Now I have created a method that will handle the convertion of UTC Date to PlainDate
public static string UTCtoPlainDate(string dateUTC)
{
if (string.IsNullOrEmpty(dateUTC)) return "";
DateTime dateTime;
// Assume date in UTC format, e.g. '2015-03-31T12:00:00.000Z'
DateTime.TryParseExact(dateUTC, "yyyy-MM-dd'T'HH:mm:ss.fff'Z'", System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.AssumeUniversal, out dateTime);
return dateTime.ToString("yyyy-MM-dd") ?? "";
}
I have a date field which contains data coming in from the database as 2015/07/31 13:01:53.180z.
Datetime is stored in UTC on database.
My code looks like this:
var startDateTime = Ext.util.Format.date(StartDateTime, 'm/d/y g:i:s A');
But the output I get is the conversion of UTC to IST(Indian).I checked on Chrome,Mozilla and IE.
I got same output all the time
Does ExtJs does this? Because I haven't wrriten any method for conversion.
I use ExtJs 4.1.1
I would appreciate any help on this.
Timezone is appended in the string->JS Date conversion.
To parse the date from database without timezone conversion you should use the Ext.Date.parse explicitly, not automatically through model field type 'date' or simply JS constructor new Date().
For example:
var db_date = '2015/07/31 13:01:53.180z',
js_date = Ext.Date.parse(db_date.substring(0,db_date.length-5), 'Y/m/d H:i:s'),
date_to_show = Ext.util.Format.date(js_date, 'm/d/y g:i:s A');
Obviously "substring" must be replaced by something better, for example you could format db date (cutting timezone part) in the web service serialization.
If you achieve to clean the date string in the web service you can also add "dateFormat" attribute to model fields to parse date correctly into models.
I want to create a Date object in Javascript using this string 04/21/2014 12:00p
When passed to the constructor (new Date('04/21/2014 12:00p')), it returns Invalid Date.
I've seen other posts which manipulate the string in order to fulfill the requirements of a valid dateString, however that is not what I want. I want Javascript to recognize my date format (m/dd/yy h:mmt). In Java, something like that is simple, I imagine that there would be a similar way in Javascript.
How can I get the Date object to recognize my format?
This is trivial only when using a library like moment.js:
var dt = moment("04/21/2014 12:00p","MM/DD/YYYY h:mma").toDate();
Otherwise, you would have considerable string manipulation to do. Also you would have to account for users in parts of the world that use m/d/y or other formatting instead of the y/m/d formatting of your input string.
If this string is being sent from some back-end process, you might consider changing the format to a standard interchange format like ISO-8601 instead. Ex. "2014-04-21T12:00:00"
To manipulate the string in order to fulfill the requirements, could be a way, but you need to take care of all browser issues.
A more quick and dirty way is use moment.js library. It helps on formatting matters too.
if (String.prototype.dateFromJava == null)
{
String.prototype.fromJava = function (sDateString)
{
var aDateOrTime = sDateString.splt(" ");
var aDateParts = aDateOrTime[0].split("/");
var aTimeParts = aDateOrTime[1].split(":");
var oDate = null;
/* just get the pieces and passing them in to new Date(), return oDate */
}
}
I get a batch report with an odd date format of dd.mm.yyy and I would like to automatically be able to convert them all to something google understands is a date, like mm/dd/yyyy. Any help would be awesome. I am a n00b with regex.
function myFunction() {
var doc = DocumentApp.getActiveDocument();
var text = doc.editAsText();
// Change up the date format
text.replaceText("c?c.c?c.cccc", "/");
}
you could split the date based on the delimiter and then mash them back together how you want with something like this:
function myFunction() {
text = '12.03.012'
textArray = text.split('.')
text = textArray[0]+'/'+textArray[1]+'/2'+textArray[2]
Logger.log(text)
}
Logging Output shows:
12/03/2012
I would use momentjs for that. That way you can go directly to a javascript date object, and don't have to mess around with regExps. You can use their parsing string-format api to convert your format to standard.
var legitDate = moment(oddlyFormatedDate, "MM-DD-YYYY"); // use MM DD etc to describe your odd date format.