How to check if a string date has passed a certain date - c

I have these strings:
yy = "19";
mm = "05";
dd = "31";
These represent the creation date of a certain object in my project. This object expires after one month. How do I check if the object has expired already?
(I came across this solution but thought there might be another way to do it.)
UPDATE: the string date apparently represent the actual expiry date

I ended up using the date format "yymmdd" so I can convert it to type long and do a simple number comparison.
sprintf(buffer, "%s%s%s", yy, mm, dd);
expiryDate = atol(buffer);
// get current date of format "yymmdd" as well
// getCurrentDate() is my function that gets the date from my SDK
currentDate = getCurrentDate();
if(expiryDate >= currentDate)
{
// expired object!
}

Related

Date/Time Formatting in React

I am working with an API that returns Dates in the format 2022-03-01T11:32:37
Created: {this.props.proposal.OPENWHEN}
Created: 2022-03-01T11:32:37
How do i format this into DD/MM/YYY 24:00:00 ?
Thanks In Advance
Something like the following should be enough:
// Example expected argument: 2022-03-01T11:32:37
function prettifyDateTime (str) {
// Splitting the string between date and time
const [date, time] = str.split("T");
// Assuming 03 is the month and 01 is the day – otherwise, those could be swapped
const [year, month, day] = date.split("-")
// Added slashes and the space before the time
return `${day}/${month}/${year} ${time}`
}
prettifyDateTime("2022-03-01T11:32:37") // '01/03/2022 11:32:37'
Otherwise, I recommend using a date library like date-fns, dayJS, or moment.
You can use the Date object to accomplish what you want. It'll also accept other date formats. Here is one place to find Date description. You also get the benefit of rejecting invalid date formats
function prettifyDateTime(str) {
let date = new Date(str);
// There's other ways of formatting this return
return date.toLocaleDateString() + ' ' + date.toLocaleTimeString();
}

React - read dates from excel file

I am having a weird issue when I was reading dates from excel files in a react application. When I read it, it comes out as some form of float but what's weird is that it only does this for 1th to 12th date of the month from 13th till 31th is fine.
What I mean is that a date like 01-01-81 gets converted to 29587.00013888889
but 13-01-81 remains in its original.
I found this one solution from here How to read date from an excel file in JavaScript. But it does not give back the original value.
Really appreciate any kind of help. Cheers.
The problem was that excel has its own way for dates and we need to convert it, into the format we need.
This is the method I used
const ExcelDateToJSDate = (date) => {
let converted_date = new Date(Math.round((date - 25569) * 864e5));
converted_date = String(converted_date).slice(4, 15)
date = converted_date.split(" ")
let day = date[1];
let month = date[0];
month = "JanFebMarAprMayJunJulAugSepOctNovDec".indexOf(month) / 3 + 1
if (month.toString().length <= 1)
month = '0' + month
let year = date[2];
return String(day + '-' + month + '-' + year.slice(2, 4))
}
You can simply pass the excel date and change the function according to the date format you need.

How to add days to date time in Salesforce Apex?

Hi I am using Salesforce Apex,
I have a date as String as below. I need to add days to it using Apex.
String dateTime = '2017-07-08T23:59:59Z';
If I add one day to it then it should be 2017-07-09T23:59:59Z as string. How will I do this?
Thanks!
Beware the DST issue! The "addDays" function is not DST-aware, so if you step over a DST transition during the addition of days (in a time zone that has DST) then the time will be messed up.
To resolve this one split the date/time into separate date and time parts first, add the days to the date part then re-combine at the end, like:
DateTime dt = ...;
Integer days = ...;
Date d = dt.date().addDays(days);
Time t = dt.time();
dt = DateTime.newInstance(d, t);
If you are working in the UK (London) time zone the following anonymous Apex illustrates the issue nicely:
DateTime dt = DateTime.newInstance(2017, 10, 28, 23, 59, 59);
System.debug('Adding days directly: ' + dt.addDays(2));
Date d = dt.date().addDays(2);
Time t = dt.time();
dt = DateTime.newInstance(d, t);
System.debug('Adding days in parts: ' + dt);
You need to convert the string to a DateTime and then add days. You can format it back after
String stringDateTime = '2017-07-08T23:59:59Z';
DateTime dt = DateTime.valueOfGmt(stringDateTime);
DateTime tomorrow = dt.addDays(1);
DateTime nextMonth = dt.addMonths(1);
DateTime anniversary = dt.addYears(1);
String formattedDateTime = dt.format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');

Epoch 0 to Date conversion using momnet.js

I need to convert my epoch date to a Date object. I did this using the following code.
let abc = moment(maintenance.actualEndDate * 1000).format('DD/MMM/YYYY hh:mm');
but there is a high chance to 0 as the value for 'maintenance.actualEndDate'.
in this case the transalated date is showing the value as '01/01/1970 12:00'.
I actually need as an empty string in variable abc if the maintenance.actualEndDate is 0
I'm working on angular 4, is there any optimal solution to this?
If what you need is converting non-zero timestamps to a formatted date, and zero timestamps as empty string, a dedicated function would be nice:
function formatTimestamp(secondsSinceEpoch) {
return secondsSinceEpoch===0 ? '' : moment.unix(secondsSinceEpoch).format('DD/MMM/YYYY HH:mm');
}
//...
let abc = formatTimestamp(maintenance.actualEndDate);
(But this has nothing specific to angular)

NancyFX not serializing dates with a trailing Z to indicate UTC/Zulu

We store all of our dates in our database as UTC.
When they are returned to us from the API, they are in the following format
"createdDate":"2014-07-30T18:34:45"
But as you can see, the date doesn't have the trailing Z (which indicates to our Angular app that it's UTC / Zulu). It should look like this
"createdDate":"2014-07-30T18:34:45Z"
I do have the following setting in our Bootstrapper
JsonSettings.ISO8601DateFormat = true;
Where in my config can I ensure that there's a trailing Z for the purpose of UTC parsing?
What version of NancyFx are you using? Because in v0.23.0 or later, the JsonSerializer code has been changed to use the "o" date format instead of the "s" date format, which should give you the trailing Z that you're looking for. (But only on UTC datetimes.)
This is the commit that made this change. Note how DateTimeKind.Unspecified dates are treated as local; that might be one possible cause of your problem, if you're not explicitly creating your DateTime objects as DateTimeKind.Utc.
Below is the NancyFx code that serializes DateTime values, as it looks as of v0.23.0 (after that commit). From https://github.com/NancyFx/Nancy/blob/v0.23.0/src/Nancy/Json/JsonSerializer.cs, lines 480-518:
void WriteValue (StringBuilder output, DateTime value)
{
if (this.iso8601DateFormat)
{
if (value.Kind == DateTimeKind.Unspecified)
{
// To avoid confusion, treat "Unspecified" datetimes as Local -- just like the WCF datetime format does as well.
value = new DateTime(value.Ticks, DateTimeKind.Local);
}
StringBuilderExtensions.AppendCount(output, maxJsonLength, string.Concat("\"", value.ToString("o", CultureInfo.InvariantCulture), "\""));
}
else
{
DateTime time = value.ToUniversalTime();
string suffix = "";
if (value.Kind != DateTimeKind.Utc)
{
TimeSpan localTZOffset;
if (value >= time)
{
localTZOffset = value - time;
suffix = "+";
}
else
{
localTZOffset = time - value;
suffix = "-";
}
suffix += localTZOffset.ToString("hhmm");
}
if (time < MinimumJavaScriptDate)
time = MinimumJavaScriptDate;
long ticks = (time.Ticks - InitialJavaScriptDateTicks)/(long)10000;
StringBuilderExtensions.AppendCount(output, maxJsonLength, "\"\\/Date(" + ticks + suffix + ")\\/\"");
}
}
As you can see, requesting ISO 8601 date format will get you the 2014-07-30T18:34:45 format rather than the number of milliseconds since the epoch, but it will assume local times if the value being serialized has a Kind equal to DateTimeKind.Local.
So I have two suggestions for you: upgrade to v0.23 of NancyFx if you're still on v0.22 or earlier (v0.22 used the "s" date format, which does not include timezone info, for serializing DateTime values). And if the DateTime objects you're serializing aren't explicitly set to DateTimeKind.Utc, then make sure you specify Utc (since the default is Unspecified, which NancyFx treats as equivalent to Local).

Resources