How to format abd get empty time with it with moment when it doesn't exist in string? - ecmascript-6

I have a use case where I have variable data in the following formats. When the string doesn't have time, the format object formats it with 12:00:00 => 12:00 am.
Well technically, the constructor that passes only a date IS instantiating it with a time, 12:00:00, even though it is implicit.
Example
'2021-02-12'
'2021-02-12T6:00'
'2021-02-19T18:00'
Code
const date = moment("2021-02-12");
date.format("h:mm a"); // 12:00 am
Expected
I'd like to know if that is possible that moment format the time with the empty string "" when time is not available in the data. e.g.
const date = moment("2021-02-12");
date.format("h:mm a"); // ""

Related

Google Script taking date format YYYY-MM-DD HH:MM:SS as text, need to change timezone

I have a response which is returning the dates as YYYY-MM-DD HH:MM:SS format. However, due to timezones issues, I am trying to convert these in to GMT format.
I tried directly converting them but Google script always takes these as Text and thus returns Jan 1, 1970 as the output when using a Date object.
I tried adding the timezone offset to the string and then converting, but to no avail.
Is there any way I can convert this format into a proper date or directly convert it into GMT?
dateValue = '2019-02-20 18:30:00'
var tempDate = dateValue + ' +0530';
dateValue = new Date(tempDate)
I'd expect the outcome to be returned as a date, which I can then convert to GMT format
How about this?
function splitItUp() {
var dts="2019-02-18 19:52:14";
var tA=dts.split(' ');
var tD=tA[0].split('-');
var tT=tA[1].split(':');
var dt=new Date(Number(tD[0]),Number(tD[1])-1,Number(tD[2]),Number(tT[0]),Number(tT[1]),Number(tT[2]))
Logger.log(dt);
}

How can I get the timestamp in “yyyy/MM/dd hh:mm:ss.fff"” format in VBA?

I want to get this output:
2018-09-02 00:00:00.000
I tried the below code:
.Cells(LRS + 1, 15).Value = Format(.Cells(LRS + 1, "A").Value, "yyyy-MM-dd hh:mm:ss.fff")
And I got:
2018-09-02 00:00:00.fff
The initial date in Excel has the following format yyyy/mm/dd, no time included. That's why the time part includes only zeros 00:00:00.000. The reason I want to include the time in the specific format is that I'm planning to import those dates into a SQL table with that format.
Is there any solution?
As you can see from the documentation fff is not recognised as a formatting token in VBA.
Helpfully, you can actually import your data into SQL without formatting it to add the time. If you import it into a datetime field the SQL engine will automatically default the time part of the field to midnight on the date you give it.
I think you can just change your format string to yyyy-MM-dd by itself.
However if you really want to do it like this, then since there's no time specified then just hard-code 000 instead of fff. The rest of the time can be similarly hard-coded, since it never varies, so you end up with yyyy-MM-dd 00:00:00.000. But as I said, I think it's a bit pointless.
After replacing the cell format with the corresponding format, it is likely that the value of the cell is imported as text, not as a value.
Sub test()
Dim s As String, s1 As String, s2 As String
'First Cell format as your "yyyy-mm-dd hh:mm:ss.000"
Range("a2").NumberFormatLocal = "yyyy-mm-dd hh:mm:ss.000"
'In vb,This format("yyyy-mm-dd hh:mm:ss.000") is not recognized.
Range("b2") = Format(Range("a2"), "yyyy-mm-dd hh:mm:ss.000")
s1 = Format(Range("a2"), "yyyy-mm-dd hh:mm:ss.000")
's1 = "2018-09-03 01:24:33.000"
'Since you format a2 cell as "yyyy-mm-dd hh:mm:ss.000" you can get data as text
Range("b2") = Range("a2").Text
s2 = Range("a2").Text
's2= "2018-09-03 01:24:33.240"
End Sub
Sheet Data
Local Window

Milliseconds to DateTime in WP8 not working

I need to convert a date in milliseconds to a user readable date and I can't.
My date is: 1494519599999 which corresponds to: Thu May 11 2017 18:19:59 GMT+0200
I need to get something like dd/mm/yyyy HH:mm:ss
I have tried what I read in other stackoverflow posts:
DateTime date = new DateTime(long.Parse(ticks));
date.ToString("yyyy-MM-ddThh:mm:ssZ");
But I always get 0 for year, month and more. I tought I should multiply or divide by 1000 the number but it was worst.
What is the correct way to do this?
The timestamp seems to be the value often used in JavaScript to represent a date (the number of milliseconds elapsed since January 1 1970). You can convert that to a DateTimeOffset using this code:
var timestamp = 1494519599999;
var epoch = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);
var dateTimeOffset = epoch.AddMilliseconds(timestamp);
The result is 05/11/2017 16:19:59 +00:00.
I am using a DateTimeOffset to make it easier for you to change the offset from +0000 to +0200. You can change the offset using this code where I assume you want to convert to local date and time. Windows Phone 8 does not support the full functionality of the TimeZoneInfo class so if you need to convert to another time zone you need create code the explicitely changes the offset:
dateTimeOffset = dateTimeOffset.ToLocalTime();
Now the result is 05/11/2017 18:19:59 +02:00.
If you prefer to use DateTime instead of DateTimeOffset you can use this code to create a UTC DateTime:
var timestamp = 1494519599999;
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
var dateTime = epoch.AddMilliseconds(timestamp);
The result is 05/11/2017 16:19:59 and if your local timezone has a offset of +0200 you can convert it to a local DateTime to get the desired result. However, when time zones are involved I suggest that you use DateTimeOffset.
Try this:
var time = TimeSpan.FromMilliseconds(milliseconds);

Format JSON date before pushing into ko.observableArray

I am pushing Values into a ko.observalbeArray with an AJAX call,
I want to format the JSON return date to "YYYY-MM-DD" before I am pushing it into my observableArray.
The Specific element in my Code that I want to convert is: OrderTimeStamp: element.OrderTimeStamp
Here is an example of a date that gets returned from server:
/Date(1377200468203+0200)/
Here is my AJAX call:
$.ajax({
url: "/[URL TO API Method]/GetAllOrdersbyparm",
data: {Parm: ko.toJS(MyDataViewModel.SelectedParmater), Start: ko.toJS(MyDataViewModel.ParmStart), End: ko.toJS(MyDataViewModel.ParmEnd)},
type: "GET",
contentType: "application/json; charset=utf-8",
dataType: "JSON",
timeout: 10000,
success: function (Result) {
for (var i = 0; i < Result.d.length; i++) {
element = Result.d[i];
MyDataViewModel.OrderDetails.push({ OrderID: element.OrderID, OrderGUID: element.OrderGUID, OrderTimeStamp: element.OrderTimeStamp, OrderStatus: element.OrderStatus, QtyProductsOnOrder: element.QtyProductOnOrder, PaymentDate: element.PaymentDate });
}
},
error: function (xhr, status) {
alert(status + " - " + xhr.responseText);
}
});
So, this is an ASP.NET specific Microsoft Date "standard".
See
http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx
why it should be avoided like the plague(1).
In that format the first component is a UTC milliseconds offset since the UNIX epoch.
The offset is TO local time, which is the opposite of the timezone offset in the JS Date string representations.
Use
var dateString = myDate.toJSON();
to serialize a JS Date object for sending.
Such a serialized datetime string, which is also in UTC (aka *Z*ulu), can be used to create a Date object thusly:
var myDate = new Date(dateString);
(1) In case you need to support this old ASP.NET Date format you can convert it to a proper JS Date like this (thanks [Roy Tinker][2]):
myDate = new Date(parseInt("/Date(1377200468203+0200)/".substr(6)));
I'm not familiar with that particular datetime notation.
Is that home-grown?
Is there documentation for that?
If not, then you are setting yourself up for trouble trying to interpret it.
That conversion toJSON would make it a UTC time and a few things are open for interpretation, unless documented in minute (no pun intended) detail.
So this is my answer: Be very sure you have the above definition in normative writing.
Now let me ramble on for a bit:
I went through that little exercise here...
http://en.wikipedia.org/wiki/ISO_8601 would be a good standard to base datetime notation on.
Now, you already get that format from the server, which looks like a millisecond time value since the epoch ('1970-01-01T00:00:00Z'), (probably with a timezone offset already applied to it!) combined with a timezone offset string in HHMM.
That's a bit scary, since those two components don't mix well.
Evaluated as an expression 1377200468203+0200 would subtract octal! 200 milliseconds! from 1377200468203. That's clearly not what's intended.
In ISO8601 (which this notation is not) this timezone offset would be FROM UTC, so the millisecond value would already have the 2 hour, 0 minutes offset applied to it.
Now the code could of course run on a machine which is in a different timezone than the datetime given.
The very crucial question is whether this millisecond datetime value has indeed the offset FROM UTC in it.
In that case, doing
var dt = new Date(1377200468203);
would be wrong.
If it is close to a daylight savings time switch time, it would be incorrect to just subtract to offset from it.
Note, not sure if below answers your question. If not, you may be helped by this one: How to format a JSON date?
Something along these lines should work:
var yyyy = element.OrderTimeStamp.getFullYear()
var mm = element.OrderTimeStamp.getMonth();
var dd = element.OrderTimeStamp.getDate();
var x = yyyy + '-' + (mm < 10 ? '0'+mm : mm) + '-' + (dd < 10 ? '0'+dd : dd)
element.OrderTimeStamp = x;
See this fiddle for an example. For reference, the MDN page has good documenation on Dates.
If you need more advanced date and time functionality I can recommend looking at MomentJS.

DateField: selectedDate shows hours as 24:00:00

With a DateField component, the selectedDate.getHours returns as 24:00:00. I want it to return as 00:00:00.
Is there an easy way to do this?
Thanks!
UPDATE:
First, I set a variable in my Model that equals the selectedDate of a DateField component:
model.generalInfo.endDate = endDate_df.selectedDate;
Then I set another variable based on that value and I trace it out:
param.todate = df.format( model.generalInfo.endDate.toString() );
And this is where I see the time equal to 24:00:00
you could try something like
selectedDate.time = selectedDate.time - 24 * 60 * 60 * 60 * 1000
as a Date.time represents miliseconds since 1970 or whatever.. you substract 24 hours..
if it not works for you, you can create a new function or getter that converts it, or you can create a new mxml module, with DateField as superclass, and you can override the getHours method. tons of options to do this..
It looks like you are using the Flex DateFormatter to format the Date object. Have a look at the docs for this class, it has a formatString property that you can use to control how to output the date (or in this case the time).
If you give the DateFormatter a format string that contains "H" will output the hour in 24 hour format using the number range 1-24. If the format string contains "J" it will output the hour in 24 hour format using the range 0-23.
To get your desired output, use "JJ" in the format string, in addition to any other items. For example to output the hours, minutes, seconds:
var someDate:Date = new Date(2012, 11, 5);
var df:DateFormatter = new DateFormatter();
df.formatString = "JJ:NN:SS";
var formatted:String = df.format(someDate); // 00:00:00
Also, as #Flextras mentioned, there is Flash localization API you can use which has the added benefit of converting date/time strings to the values used by their locale. These are the DateTimeFormatter classes:
fl.globalization.DateTimeFormatter
spark.formatters.DateTimeFormatter (Flex 4)
These classes format dates into the user's locale (or one that you specifcy), and format Date objects the same way the DateFormatter does.