I am trying to create a moment object from string which contains date in format DD/MM/YYYY. I want to set this to be DD/MM/YYYY 23:30. For this, I am writing the code as
moment.utc('30/04/2016','DD/MM/YYYY').hours(23).minutes(30)
However, that for some reason sets the milliseconds to 300. I am unable to set milliseconds to 0 even using the code below
moment.utc('30/04/2016','DD/MM/YYYY').hours(23).minutes(30).milliseconds(0)
I only need my date to be like 30/04/2016 23:30:00 and not like 30/04/2016 23:30:00:300
Please see JS Fiddle
Why is the millisecond part being set to 300 and if why am I not able to set it to 0?
Use this
moment.utc('30/04/2016 23:30:00 0','DD/MM/YYYY HH:mm:ss SSS')
See this http://momentjs.com/docs/#/parsing/string-format/ & http://momentjs.com/docs/#/displaying/
Try this,
<body>
<p>Click the button to display date</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var d = new Date();
var n=moment.utc('30/04/2016','DD/MM/YYYY').hours(23).minutes(30).seconds(0).milliseconds(0).format('DD/MM/YYYY hh:mm:ss.SSS');
document.getElementById("demo").innerHTML = n;
}
</script>
</body>
working fiddler,
http://jsfiddle.net/fhdd8/356/
Well u can set time according to your choice by default .now() add upto millisecond.
var datetime = moment().set({ hour: 10, minute: 22, second : 19 , millisecond : 999 });
var datetime2 = moment().hour(23).minute(2).second(20);
var time3 = moment('23:11:22.322', "LTS.SSS", true);
datetime.format(LTS.SSS);
datetime2.format(LTS);
datetime.format("hh:mm:ss.SSS")
LTS - Localized formats
Time LT 8:30 PM
Time with seconds LTS 8:30:25 PM
SSS - Milisecond*s
For display use format()
Related
So my question is pretty simple. I'd like to have specific dates that are enabled on a datepicker, like only the last day of any month, for example 30/06/2019, 31/07/2019
Primng Datepicker
<p-calendar formControlName="date"></p-calendar>
It is a simple hack for your requirement.
In your component.html use min and max date and (onMonthChange) method to update min and max value of date when user changes the month. Trick is just make min and max date equal to last day of the month.
<p-calendar [(ngModel)]="value" tabindex="0" [maxDate]="maxDateValue"
[minDate]="minDateValue" readonlyInput="true"
(onMonthChange)="onMonthChange($event)">
</p-calendar>
And in component.ts file use below code:
public maxDateValue: Date;
public minDateValue: Date;
ngOnInit() {
this.setMinMaxDate();
}
setMinMaxDate() {
var nowdate = new Date();
this.maxDateValue = new Date(nowdate.getFullYear(), nowdate.getMonth() + 1, 0);
this.minDateValue = new Date(nowdate.getFullYear(), nowdate.getMonth() + 1, 0);
}
// method to handle month change.
onMonthChange(e:any) {
this.minDateValue = new Date(e.year, e.month, 0);
this.maxDateValue = new Date(e.year, e.month, 0);
}
You must be thinking why at time of initialization I am using nowdate.getMonth() + 1, but inside onMonthChange only e.month, not adding + 1.
You need to go through this find last and first date of month article how to get the last and first date of a month and print the values of month in console and see the differences. Then you will understand easily.
If you just need one day of each month you should use the month view
<p-calendar [(ngModel)]="dateValue"
view="month" dateFormat="mm/yy"
[yearNavigator]="true"
yearRange="2000:2030"></p-calendar>
I have date from datetimepicker in this format "14-02-2018". Value is string.
I must parse this value to date. I try this way:
new Date(Date.parse('02-03-2018))
But it works only if the format is 2018-03-02
In moment I can't parse too.
var aaa = moment(itemvalue, "DD-MM-YYYY");
Using Moment JS
var date = new Date(moment('02-03-2018', "DD-MM-YYYY"));
console.log(date.toString());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
What is the best way to determine the correct date format (dd/mm or mm/dd) of a Google Form response.
When I use the namedValues object:
function onFormSubmit(e){
var namedValues = e.namedValues;
var date = namedValues['Date']; // Date=[05/06/2018]
var date = new Date(date);
Logger.log(date); //Sun May 06 00:00:00 GMT+10:00 2018
}
When I use the value from the spreadsheet:
function onFormSubmit(e){
var range = e.range;
var row = range.getRow();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Form Responses 1');
var date = sheet.getRange(row,2).getValue();
Logger.log(date); //Mon Jun 05 00:00:00 GMT+10:00 2018
}
I don't know whether I should be, but I am hesitant using the values from the spreadsheet in an onFormSubmit trigger, since I have experienced instability in the past where I think the trigger was running before the data was being posted to the spreadsheet.
I cannot find anything in the Google Forms documentation stating whether a date response is always in a consistent format. If it was always dd/mm/yyyy I could construct the date using the string parts.
Is there a way to use determine the correct date format from the namedValues object?
P.S I would rather not use the moment.js library for this one requirement, so keen to understand if its possible without.
You Google form will constantly submit the date in the same format, but your sheet might change the appearance of formatting.
Also, there is now a date format utility built into Apps script. You can change the date string to the format that you need.
// This formats the date as Greenwich Mean Time in the format
// year-month-dateThour-minute-second.
var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd'T'HH:mm:ss'Z'");
Logger.log(formattedDate);
how can i make timepicker in form return the time i chose .
TimePicker in form :
<label class="padd"> Time of visiting </label>
<ngb-timepicker [(ngModel)]="meridianTime" [meridian]="meridian"
formControlName="time" id="time" ></ngb-timepicker>
TypeScript part:
defaultTime = {hour: 13, minute: 30};
customTime: NgbTimeStruct = {hour: 13, minute: 30, second: 0};
hourStep = 1;
minuteStep = 15;
You have time. Here is docs: link
You have time as hour, minute, second props. You can convert it to whatever you need. To convert you can use internal JavaScript API: Date or some libraries like moment. Or something like this: https://stackoverflow.com/a/27979128/2898694 :)
So you have date in timepicker format. As object I mean. You can write something like this:
prepareDate(dateAsObject) {
return dateAsObject.hour + ':' + dateAsObject.minute + ':' + dateAsObjet.second;
}
This is first thing in my head. Of course you can prepare date as you wish.
How to get date values in month, year and days in grails datePicker
<g:datePicker id="test" name="test" precision="day"></g:datePicker >
I tried using getDate() but I do not get any value.
Try this approach, first register change handler to each of the 3 selects generated by the datePicker tag, then call the dateWasChanged method and inside that build your date and update your mydate div.
Note: On date Object month is 0 base (index) so you need to deduct 1 unit from the actual select value.
<body>
<g:datePicker id="test" name="test" precision="day"></g:datePicker >
<div id="mydate"></div>
<script type="text/javascript" charset="utf-8">
dateWasChanged()
$( "#test_day" ).change(function() {
dateWasChanged();
});
$( "#test_month" ).change(function() {
dateWasChanged();
});
$( "#test_year" ).change(function() {
dateWasChanged();
});
function dateWasChanged(){
var year = $( "#test_year" ).val();
var month = $( "#test_month" ).val()-1;
var day = $( "#test_day" ).val();
var date = new Date(year,month,day);
$("#mydate").text(date.toString())
}
</script>
</body>
Try something like the following:
Date myDate = params.date('test', 'dd-MM-yyyy')
That will give you a good ol' fashioned java.util.Date object with Groovy extensions that you can use from there. Be sure to set the format (in my case 'dd-MM-yyyy') to whatever date format you are using.