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();
Related
How to format 2019-07-29T05:55:00 to 29 Jul 2019 (Mon) in moment js ? What should be the formatdate string ? 'DD MMM YYYY (ddd) is not accepted by moment js
I'm not allowed to comment yet, but I when I saw your question I was sure your format was correct.
var now = moment().format('DD MMM YYYY (ddd)');
Tried it out in JS fiddle and it works.
It should be working fine, the correct syntax:
var test = moment().format('DD MMM YYYY (ddd)');
var myDate = moment('2019-07-29T05:55:00').format('DD MMM YYYY (ddd)');
result.innerHTML = myDate ;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.2/moment.js"></script>
<pre id="result"></pre>
[enter image description here][1]I need assistance in momentjs.
I’m using momentjs for date and time formats.
The issue I’m facing is that momentjs is displaying the current date and time instead of the correct date and time that is stored in the API.
Below is my code:(eventStartUTC) is the stored time in the API.
var startTime = moment(item.eventStartUTC).format("HH:mm" + " - ");
var endTime = moment(item.eventEndUTC).format("HH:mm");
var finalTime = startTime.concat(endTime);
Values for eventStartUTC and eventEndUTC as stored in the API:
item.eventStartUTC - "26/11/2017 06:00:00",
item.eventEndUTC - "28/11/2017 15:00:00"
date and time api values: https://i.stack.imgur.com/6ajO3.png
As moment(String) states:
When creating a moment from a string, we first check if the string matches known ISO 8601 formats, we then check if the string matches the RFC 2822 Date time format before dropping to the fall back of new Date(string) if a known format is not found.
Since input (item.eventStartUTC and item.eventEndUTC) is neither in ISO 8601 nor RFC 2822 you have to use moment(String, String).
Here a live sample:
var item = {
eventStartUTC: '26/11/2017 06:00:00',
eventEndUTC: '28/11/2017 15:00:00'
};
var startTime = moment(item.eventStartUTC, 'DD/MM/YYYY HH:mm:ss').format("HH:mm" + " - ");
var endTime = moment(item.eventEndUTC, 'DD/MM/YYYY HH:mm:ss').format("HH:mm");
var finalTime = startTime.concat(endTime);
console.log(finalTime);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
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',
});
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 am facing a problem in date format. I am converting a string date(dd/MM/yyyy) to datetime, using convert.toDateTime(). It works fine on my local machine but causes problem when I run from server. So to set uniformality, I tried to set culture info for (yyyy-MM-dd HH:mm:ss) format, but couldn't set as .net shows an error.
I tried like this.
CultureInfo DateInfo = new CultureInfo("yyyy-MM-dd HH:mm:ss");
How can I set the culture info for this format?
Try to use like this...
string dateString = "Mon 16 Jun 8:30 AM 2008"; // Modified from MSDN
string format = "ddd dd MMM h:mm tt yyyy";
DateTime dateTime = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);
Response.Write(dateTime);
hope this may helpful...