If I have a datetime string that come from my database e.g. 2016-02-07 00:00:00
how can I get my input field to display this using format 'DD MMM YYYY'
I have moment.js defined and the following pikday instance but the default set value doesn't display as I want it. Instead the input field displays it as 20 Jan 0000. How can you get pikaday to display any valid datetime string in the format that you want?
<input id="start_dt" type="text" value="2016-02-07 00:00:00" name="start_dt">
var start_dt = new Pikaday(
{
field: $('#start_dt')[0],
format: "DD MMM YYYY"
}
You will have to set the default value to the one that you have.
First up get the date value from the input.
var date = $( '#start_dt' ).val();
And then,
var picker = new Pikaday({
field: document.getElementById('datepicker'),
defaultDate : moment(date).format("DD MMM YYYYY"),
format: 'DD MMM YYYY',
});
Related
I'm working with IONIC2, Angular2 and Typescript. I have an Datetime working as follows:
page.html
<ion-datetime displayFormat="DD MMMM YYYY" pickerFormat="DD MMMM YYYY" [(ngModel)]="date"></ion-datetime>
page.ts
date: string = new Date().toISOString();
The ion-datetime field shows time with an hour less, how can I display date on Datetime picker considering the timezone?
Reading this answer I solve my problem. Finally I use:
moment(new Date().toISOString()).locale('es').format();
Thanks to sebaferreras
the simplest is to remove timezone offset in milliseconds:
date = new Date();
myDate: String = new Date(this.date.getTime() -
this.date.getTimezoneOffset()*60000).toISOString();
I am working with date function.data comes in different format like
1."2016-01-31"
2."31-01-2016"
3."31/01/2016"
4."2016/01/31"
5."jan 01 2016"
6."2016-01-02 12:00 AM"
Now I want to convert all above mentioned format date to DateTime format YYYY-mm-dd.
I tried many methods but some format shows error message like
'String was not correct format'
'Unable to convert string value to datetime'
I tried,
Datetime date=DateTime.ParseExact(txtdate.Text,'YYYY-mm-dd',CultureInfo.InvariantCulture);
How can I cast/Convert value to datetime format(YYYY-mm-dd) from any format text box value.
mostly I am geting error afterI upload to server(GoDaddy and Microsoft azure).
Can I Use like this
string dateTimes=txtdate.Text;
string[] dateTimes = new string[] { "YYYY-mm-dd", "dd-mm-YYYY","mm/dd/YYYY","yyyy/MM/dd"};
Your code isn't quite right. The date format string should be surrounded by " (double quotes) and the casing should be like so "yyyy-MM-dd". Also CultureInfo.InvariantCulture is spelt incorrectly.
Having said that, you shouldn't need to use the ParseExact function to convert the string to DateTime format. You can simply use Convert.ToDateTime
Here's some sample code I used to test the Convert.DateTime function:
string[] dateTimes = new string[] { "2010-02-01", "02-03-2011", "03/04/2012", "2013/05/04", "june 05 2014", "2015-07-06 11:00 AM" };
StringBuilder sb = new StringBuilder();
foreach (string date in dateTimes)
{
DateTime dt = Convert.ToDateTime(date);
sb.AppendLine(dt.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
}
string convertedDateTimes = sb.ToString();
Using the relevant parts from above, you can change your code to the following:
try
{
Datetime date = Convert.ToDateTime(txtdate.Text.Trim());
string dateString = date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
}
catch (FormatException ex)
{
//Handle the exception. e.g. show a message or print something to the form.
}
My task is to use a datepicker to pick a date in the prescribed format, eg(MM-DD-YYYY) and pass it to the server as ISO formatted.
While it test the output the ISO formatted date is one day behind.
For example
If i select
07-13-2015
My Output ISO format is
ISO format is :2015-07-12T18:30:00.000Z
Here you can see date is 13 but the output date is 12
I am from India. I tried with zone and utcOffset, ended up with no results. How do i set it right
Here is the JSFIDDLE
js code
$('#datetimepicker1').on("dp.change",function(e){
var selectedDate = $('#datetimepicker1').find("input").val();
selectedDate = moment(selectedDate,"MM-DD-YYYY");
$(".temp").text(moment(selectedDate).toISOString());
});
I do have a hidden field which value will be updated on change and that will be processed in the server. No issues on that.
$('#datetimepicker1').on("dp.change",function(e){
var selectedDate = $('#datetimepicker1').find("input").val();
selectedDate = moment(selectedDate,"MM-DD-YYYY");
$(".temp").text(selectedDate.toISOString());
});
Your selectedDate is already a moment object so you do not need to feed it back into another moment.
Example:
var test = '07-13-2015'
var mtest = moment(test,"MM-DD-YYYY")
mtest.toISOString()
"2015-07-13T06:00:00.000Z"
Your could try converting the date format to UTC at once.
selectedDate = moment(selectedDate).utc('MM-DD-YYYY')
According to http://dygraphs.com/date-formats.html, if you pass a string like '07-13-2015', it means Midnight of 13th July 2015. Now, if you use toISOString function, it will convert it to UTC by default. To not convert it to UTC, just pass a parameter true in the toISOString function. (Moment.js docs)
For example:
var date = '07-13-2015';
date = moment(date,'MM-DD-YYY');
console.log(date.toISOString(true));
This way, moment will not convert the date to UTC.
i want to parse any datetime format to dd/MM/yyyy format.
here is my code
// dates i am providing are
// Sat, 01 Oct 2011 17:30:00 +0400
// and
// Sat, 01 October 2011 12:21:23 EST
Datetime dt = Convert.toDateTime(pubDate);
which is giving me following exception
The string was not recognized as a valid DateTime. There is an unknown word starting at index 32
any one guide me how can i parse any dateformat to a single one?
any help would be appreciated.
DateTime doesn't store dates in a "format" - it uses an internal representation. You need to parse a passed in string in order to get the correct value for the DateTime and when you want to display it you can then format it to whatever display.
Your best bet is to use TryParseExact supplying it with the exact format string. You need to use the custom Date and Time format strings with it.
Use the overload that takes a string[] of format strings - one for each date format.
In regards to the EST portion - the framework doesn't have support for named timezones. You may want to write a wrapper that converts named timezones to their equivalent but parseable form.
Untested (based on MSDN example):
string[] formats= {"ddd, dd/MMM/yyyy hh:mm:ss K",
"ddd, dd MMMM yyyy hh:mm:ss EST"};
DateTime dateValue;
foreach (string dateString in dateStrings)
{
if (DateTime.TryParseExact(dateString, formats,
new CultureInfo("en-US"),
DateTimeStyles.None,
out dateValue))
Console.WriteLine("Converted '{0}' to {1}.", dateString, dateValue);
else
Console.WriteLine("Unable to convert '{0}' to a date.", dateString);
}
Remove the 'EST' from the string and it should work.
How to validate particular format date string using Javascript?
I have one date picker which has the display format like "dddd MMMM dd, yyyy"(displaying like this:"Wednesday February 03, 2010".) So i have to validate this format using javascript. Please help me for implementing this..
If you want to check exactly that format, you could use regular expression:
var re = new RegExp( '^(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)\\s*(January|February|March|April|May|June|July|August|September|November|December)\\s*(\\d\\d),\\s*(\\d{2,4})$' );
var date = 'Wednesday February 03, 2010';
if ( ( match = date.match( re ) ) != null )
{ // valid
alert( match );
}
Or if you just need to know if it is a valid date, what format ever, simply convert it:
var dateSec, dateObj, dateStr = 'Wednesday February 03, 2010';
dateSec = Date.parse( dateStr ); // unix timestamp
if ( dateSec ) // not NaN
dateObj = new Date( dateSec ); // date object
If your application is going to require date manipulation methods, you may want to consider using something like the Datejs library.
If you opt for Datejs, you can use the parseExact() method for the validation. It will return a date object if the date is valid, or null if the date is invalid.
Native JavaScript support for date formatting and validation is somewhat limited.
Take a look at http://www.datejs.com/
You can do stuff like Date.parse('my date string')
Datejs or Dojo can do this. With dojo.date.locale.parse:
var dateAsString = "Wednesday February 03, 2010";
var dateObject = dojo.date.locale.parse(dateAsString, {datePattern: "EEEE MMMM dd, yyyy", selector: "date", locale: "en"});
dateObject will contain the Date object, or null if the string does not match the specified pattern. This can work with a fixed language or any native language.
It doesn't seem right that a date picker would use this as a serialized Date format, though. It should use something easier to parse, like ISO8601 representation.